Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

When I have a function inside of a function I get this strange loop. Why does this happen?

Hello fellow reader, I simply have a question about functions. I am having this issue where if I have a function inside of a function, I get this type of loop where if I run function-MouseEnter around 5 times. It of course prints("Hi") 5x times. But if I then run function-MouseButton1Down 1x time. Then function-MouseButton1Down gets fired 5 times. Even though I ran it only 1 time. And if I run function 2 again it runs 5 times instead of only one.

script.Parent.MouseEnter:Connect(function()
	print("Hi")
	connection = Items.Sword.MouseButton1Down:Connect(function()
		print("Hi2")
	end)
end)

Could someone explain how to end the loop? I am really confused about functions so please tell me if there was something you didn't understand. It's kinda hard to explain this certain problem but its crucial for me to fix. Any help would be thanked

2 comments

0

Are you sure it's not because you're holding down the mouse?

0

I don't think its the issue. The script i put on this post is not similar to the one i wrote on studio. My issue is that if i run mouse enter 5x times. It creates 5 instances of mousebutton1down. But i only want it to create 1 instance no matter how many times mouseenter fires

2 answers

✓ Accepted answer

Every time the MouseEnter function activates, it creates a new instance of the MouseButton1Down function. In this case, you ran the MouseEnter function 5 times. That made the script create 5 instances of the MouseButton1Down function. When you then try to trigger the second function, all five instances activates at ones and thus "Hi2" is printed five times.

1 comment

0

Thank you thats true. Is there a way I can make it so that it only creates 1 instance even though you fire the function mouseenter 5 times?

found a solution. I was such an idiot. Instead of having the function inside, I put it outside

This discussion is preserved as it was. New answers, comments, and votes are closed.