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

How would I stop this from executing over and over?

Asked by 6 years ago

Here is the code:

local valueRequired = 100
game.Players.PlayerAdded:Connect(function(plr)
    local stat = plr:WaitForChild("leaderstats"):WaitForChild("Cash")
    stat.Changed:Connect(function(val)
        if val >= 100 then
            local tool = game.ServerStorage["SapphireCane"]:Clone()
            tool.Parent = plr.Backpack
            plr.CharacterAdded:Connect(function()
                local tool = game.ServerStorage["SapphireCane"]:Clone()
                tool.Parent = plr:WaitForChild("Backpack")
            end)
        end
    end)
end)

How would I stop making this happen so I do not get a bunch of the tool? For I get one tool every time my money changes, and I only want it to happen once.

1 answer

Log in to vote
0
Answered by
mattscy 3725 Moderation Voter Community Moderator
6 years ago
local valueRequired = 100
game.Players.PlayerAdded:Connect(function(plr)
    local stat = plr:WaitForChild("leaderstats"):WaitForChild("Cash")
    local hasGotten = false
    stat.Changed:Connect(function(val)
        if val >= 100 and hasGotten == false then
            hasGotten = true
            local tool = game.ServerStorage["SapphireCane"]:Clone()
            tool.Parent = plr.Backpack
            plr.CharacterAdded:Connect(function()
                local tool = game.ServerStorage["SapphireCane"]:Clone()
                tool.Parent = plr:WaitForChild("Backpack")
            end)
        end
    end)
end)
0
Thanks again! K1ng_Phant0m 13 — 6y
Ad

Answer this question