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

How to add a one time stat change?

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

✓ Accepted answer

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.

1 comment

0

Thanks!

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