Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 4 years ago

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

0
Are you sure it's not because you're holding down the mouse? TiredMelon 405 — 4y
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 Skydoeskey 108 — 4y

2 answers

Log in to vote
1
Answered by
sydre 229 Moderation Voter
4 years ago

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.

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? Skydoeskey 108 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

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

Answer this question