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

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

Asked by
Lodok3 18
5 years ago
Edited 5 years ago

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

Log in to vote
0
Answered by
xEmmalyx 285 Moderation Voter
5 years ago
Edited 5 years ago

When answering, if your answer does not fully solve the question, it should be written as a comment to the question instead of as an 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
2
Why is a repeat until loop better than a while loop. User#24403 69 — 5y
0
Also you did not answer the question User#24403 69 — 5y
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? xEmmalyx 285 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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

Answer this question