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

The script is firing the event over and over again when I re-equip the tool?

So this is my Code

local Tool = script.Parent --make sure this is a Tool object

local m = false

local plr = game.Players.LocalPlayer

  

  



Tool.Equipped:Connect(function(Mouse)

	Mouse.Button1Down:Connect(function()

		m = true

	end)

	Mouse.Button1Up:Connect(function()

		m = false

	end)

while wait(0.01) do

		while m == true do

			workspace.Events.AddPoints:FireServer()

			wait(2)

		end

	end

end)

2 answers

✓ Accepted answer

The reason is your loop doesn't end when m is false What you should do is this

while m do
    -- code
end

But what I would recommend more than a while loop for this is a repeat loop

repeat
wait(.01)
-- code
until not m

3 comments

2

Why is a repeat until loop better than a while loop.

0

Also you did not answer the question

0

I thought his problem was his while wait() do never ended so I recommended him to change the loop but maybe I communicated wrong?

This answer assumes you have basic knowledge of tools and their events

Hello Lodok3. :p

The problem with your tool's that each time the tools equipped, it's going to keep creating a new loop. You never set anything up to stop the one prior. Lines 27-37 yield this.

while wait(0.01) do -- Doesn't have anything set up to stop.
	while m == true do
		workspace.Events.AddPoints:FireServer()
		wait(2)
	end
end

One solution can to be to instead put the second loop (while m == true do) inside the Button1Down event. An example of this would be;

...Button1Down:Connect(function()
	MouseDown = true
	while MouseDown do
		-- Code
	end
end)

...Button1Up:Connect(function()
	MouseDown = false
end)

However, another problem arises: when the player unequips! :O Nps though, we got this covered. :p We can detect when a player unequips a tool via the Unequipped event of a tool, and so from there, when it fires, we change MouseDown to false.

Tool.Unequipped:Connect(function()
	MouseDown = false
end)

If you have any questions, feel free to ask. :) I hope this helped! :D

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