|
| 18 Jul 2016 12:26 AM |
Hey, working on a gun, and inside of my equipped function I have a UserInputService event connected to a function.
I thought this meant the event would only fire when the gun is equipped, but everytime I re-equip it, the function basically doubles and is fired twice and so on.
How should I fix this? |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 12:29 AM |
This is the code with the other contents left out:
tool.Equipped:connect(function() game:GetService("UserInputService").InputBegan:connect(function() print("test") end) end)
Test prints twice if I re-equip, then 3 times if I re-equip again and so on. |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 12:29 AM |
Well if I'm understanding your problem correctly, all you need to do is have a variable which is set to false and then as soon as the tool is equipped set it to true. Then have the function act whether it is false.
|
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 12:31 AM |
Tittle should be events within events.
Events ~= functions, please learn the difference. |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 12:31 AM |
| Okay I'll try it, but how come I can have something like a MouseButton1Down function inside it and it doesnt like fire twice when I re-equip...... |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 12:32 AM |
I know the difference, and they are functions, but they are connected to events :D.
I worded it a bit badly, sorry! |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 12:34 AM |
| But my question still stands |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 12:36 AM |
Can you create meta functions with Lua?
Does not sound like a good idea. |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 12:37 AM |
^^^You could do all that^^^ Orrrrr...
tool.Equipped:connect(function() local event = game:GetService("UserInputService").InputBegan:connect(function() print("test") end) tool.Equipped:wait() event:disconnect() end)
In my opionin, this would keep everything more organized.
--[[ rip roblox inception ]]-- |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 12:39 AM |
There ya go exo. Everything's a solved.
--[[ rip roblox inception ]]-- |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 12:41 AM |
Why not just:
local equipped = false tool.Equipped:connect(function() equipped = true end) tool.Unequipped:connect(function() equipped = false end)
game:GetService("UserInputService").InputBegan:connect(function() if equipped then -- stuff end end) |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 12:44 AM |
I'm still so confused.
Most gun scripts have a MouseButton1Down event connected to a function and it does not make the function fire more the more you equip it. Why don't I have to disconnect those too?
I tried what Flux had and I am running into problems, something is messed up. |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 12:47 AM |
@Flux Cause he tried what Flux had and he am running into problems, so something is messed up.
--[[ rip roblox inception ]]-- |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 12:47 AM |
| @Lateral I will try yours but what does tool.Equipped:wait() do? |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 12:48 AM |
| What flux has works, but you can break it by spamming the guns somehow. |
|
|
| Report Abuse |
|
|
| |
|
|
| 18 Jul 2016 12:57 AM |
"Most gun scripts have a MouseButton1Down event connected to a function and it does not make the function fire more the more you equip it. Why don't I have to disconnect those too?" Assuming you're referring to Mouse.Button1Down, it's because the tool provides you with a mouse that you connect the event to. What that means is that the mouse gets GC'd the events disconnect.
If you were to do this: local m = player:GetMouse() tool.Equipped:connect(function() m.Button1Down:connect(function() print("hi") end) end)
The event would still be connected when you unequip it, and when you reequip it now 2 events would be firing (and so on). |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 01:00 AM |
Okay, thanks, and I also figured out what was wrong withmy code using your method. I think it finally works.
|
|
|
| Report Abuse |
|
|
Cinnace
|
  |
| Joined: 21 Jun 2016 |
| Total Posts: 370 |
|
|
| 18 Jul 2016 01:47 AM |
tool.Equipped:connect(function() game:GetService("UserInputService").InputBegan:connect(function() print("test") end) end)
@war,
isn't this a function within an event within a function within an event?
- Cinnace |
|
|
| Report Abuse |
|
|
Cinnace
|
  |
| Joined: 21 Jun 2016 |
| Total Posts: 370 |
|
|
| 18 Jul 2016 01:47 AM |
elseif inputObject.UserInputType == Enum.UserInputType.MouseButton1 and not switchingWeapons and not game:GetService('UserInputService'):IsKeyDown(Enum.KeyCode.LeftShift) then
--FIRE DA CANNONNNNN
- Cinnace |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 09:19 AM |
| @Cinn Well now, if you want to get specific I'd say his problem was about 'An event connection in a function that's connected to a different event'. :) |
|
|
| Report Abuse |
|
|
Cinnace
|
  |
| Joined: 21 Jun 2016 |
| Total Posts: 370 |
|
|
| 18 Jul 2016 09:26 AM |
ikr how could he not know these things smhhhhhh
- Cinnace |
|
|
| Report Abuse |
|
|
|
| 18 Jul 2016 09:27 AM |
| or you could use the Activated event of tools ;) |
|
|
| Report Abuse |
|
|
Cinnace
|
  |
| Joined: 21 Jun 2016 |
| Total Posts: 370 |
|
|
| 18 Jul 2016 09:38 AM |
"or you could use the Activated event of tools ;)"
ew... tools
i prefer to weld models to the arm.
- Cinnace |
|
|
| Report Abuse |
|
|
darthpyro
|
  |
| Joined: 18 Aug 2009 |
| Total Posts: 3569 |
|
|
| 18 Jul 2016 09:41 AM |
| It's activating multiple times because the event is being connected every time it's equipped. |
|
|
| Report Abuse |
|
|