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

Have a stat only go down one time when the block is touched?

I have a kill script here that when the parent is touched, it kills the player, resets their streak, and sets their willpower down by 1. However, when the player touches it and dies, their body parts repeat the script x number of times, making the willpower stat going far lower than what it should be. How can I remedy this?

function onTouch(part) 
	local humanoid = part.Parent:FindFirstChild("Humanoid")
	local player = game.Players:playerFromCharacter(part.Parent)
	if (humanoid ~= nil) then
		humanoid.Health = 0
		player.leaderstats.Streak.Value = 0
		player.leaderstats.Willpower.Value = player.leaderstats.Willpower.Value - 1
	end 
end
script.Parent.Touched:connect(onTouch)

1 answer

✓ Accepted answer

You can make a debounce.

local debounce = {};
function onTouch(part) 
    local humanoid = part.Parent:FindFirstChild("Humanoid")
    local player = game.Players:playerFromCharacter(part.Parent)
    if player then -- Changed to not cause issues with NPCs
		if debounce[player] then return end;
		debounce[player] = true;
        humanoid.Health = 0
        player.leaderstats.Streak.Value = 0
        player.leaderstats.Willpower.Value = player.leaderstats.Willpower.Value - 1
		player.CharacterAdded:wait();
		debounce[player] = false
    end 
end
script.Parent.Touched:connect(onTouch)

How does it work?
When a Player touches it, it saves that the Player touched it and kills the associated Humanoid. It then waits until the Player respawns, and then lets the script run again for the player. This means that it can not be triggered multiple times in a single death.

1 comment

0

Thank you!

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