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

How to add a one time stat change?

Asked by
IsPoyo -1
5 years ago
Edited 5 years ago

I want to make a script that only adds a stat to the player who clicked the part once and can't be clicked over and over again. Is there a way for it to be player specific and doesn't effect the entire servers ability to click it?

Here is the code I have:

script.Parent.ClickDetector.MouseClick:connect(function(player) local players = game.Players:FindFirstChild(player.Name) local stat = players:FindFirstChild("leaderstats")["Kirbies Found"] stat.Value = stat.Value + 1 end)

1 answer

Log in to vote
0
Answered by
Rheines 661 Moderation Voter
5 years ago
Edited 5 years ago

To prevent multiple people from clicking/touching/activating the same object multiple times, you can make use of a debounce table. Since the first parameter of MouseClick is the player instance that clicked the part then you don't really have to use :FindFirstChild() on the Players service.

local Debounce = {}

script.Parent.ClickDetector.MouseClick:Connect(function(player)
    --Return immediately if the player has clicked the block.
    if Debounce[player] then return end

    --If they haven't clicked the block then put them on the table.
    Debounce[player] = true
    local stat = player:WaitForChild("leaderstats")["Kirbies Found"]
    stat.Value = stat.Value + 1
end)

Please accept this answer if it helps.

0
Thanks! IsPoyo -1 — 5y
Ad

Answer this question