generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Functions within functions?

Previous Thread :: Next Thread 
exoSentinel is not online. exoSentinel
Joined: 18 Mar 2012
Total Posts: 2795
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
exoSentinel is not online. exoSentinel
Joined: 18 Mar 2012
Total Posts: 2795
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
Brick_Wizard is not online. Brick_Wizard
Joined: 15 Mar 2016
Total Posts: 2072
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
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
18 Jul 2016 12:31 AM
Tittle should be events within events.

Events ~= functions, please learn the difference.
Report Abuse
exoSentinel is not online. exoSentinel
Joined: 18 Mar 2012
Total Posts: 2795
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
exoSentinel is not online. exoSentinel
Joined: 18 Mar 2012
Total Posts: 2795
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
exoSentinel is not online. exoSentinel
Joined: 18 Mar 2012
Total Posts: 2795
18 Jul 2016 12:34 AM
But my question still stands
Report Abuse
StrongNuclear is not online. StrongNuclear
Joined: 05 Mar 2016
Total Posts: 963
18 Jul 2016 12:36 AM

Can you create meta functions with Lua?

Does not sound like a good idea.
Report Abuse
LateralLace is not online. LateralLace
Joined: 19 Sep 2015
Total Posts: 662
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
LateralLace is not online. LateralLace
Joined: 19 Sep 2015
Total Posts: 662
18 Jul 2016 12:39 AM
There ya go exo. Everything's a solved.


--[[ rip roblox inception ]]--
Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
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
exoSentinel is not online. exoSentinel
Joined: 18 Mar 2012
Total Posts: 2795
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
LateralLace is not online. LateralLace
Joined: 19 Sep 2015
Total Posts: 662
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
exoSentinel is not online. exoSentinel
Joined: 18 Mar 2012
Total Posts: 2795
18 Jul 2016 12:47 AM
@Lateral I will try yours but what does tool.Equipped:wait() do?
Report Abuse
exoSentinel is not online. exoSentinel
Joined: 18 Mar 2012
Total Posts: 2795
18 Jul 2016 12:48 AM
What flux has works, but you can break it by spamming the guns somehow.
Report Abuse
exoSentinel is not online. exoSentinel
Joined: 18 Mar 2012
Total Posts: 2795
18 Jul 2016 12:54 AM
:/.
Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
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
exoSentinel is not online. exoSentinel
Joined: 18 Mar 2012
Total Posts: 2795
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 is not online. 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 is not online. 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
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
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 is not online. 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
Proxidious is not online. Proxidious
Joined: 18 Jul 2011
Total Posts: 1339
18 Jul 2016 09:27 AM
or you could use the Activated event of tools ;)
Report Abuse
Cinnace is not online. 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 is not online. 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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image