[Solved]How do you prevent an anonymous function from running multiple times?
Asked by
6 years ago Edited 5 years ago
I have a function that fires whenever a person steps on a telepad; this function displays a talk button to the player. Inside of that function is another one that runs if the player clicks the talk button.
I wanted to spruce it up a little and add some effects, but I got stumbled on the second function firing multiple times. The code "works", but I just can't seem to figure out why the function runs more than once, and in increasing increments. Here is a code snippet and output sample to show what I mean:
03 | local ChatGui = game.Players.LocalPlayer.PlayerGui.ChatGui |
04 | local Message 1 = game.StarterGui.ChatGui.InitialMessage.Value |
06 | script.Parent.Touched:Connect( function () |
12 | ChatGui.TalkFrame.TalkButton.MouseButton 1 Click:Connect( function () |
15 | for i = 1 , #Message 1 do |
18 | ChatGui.Frame.TextLabel.Text = string.sub(Message 1 , 1 ,i) |
19 | if i = = #Message 1 then |
And the output:
https://imgur.com/wOy7Dwl
The code works exactly as I expected it to the first time the TalkButton is clicked, but the function runs more than once if the player clicks it a second time. I've tried a second debounce within the MouseClicked event, but I'm pretty sure it has something to do with it creating multiple anonymous functions.
Any ideas on how to fix this will be greatly appreciated! Thank you.