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

I need help when a player touches a part in order to gain five points on the leaderboard?

local enabled = true

script.Parent.Touched:Connect(function(hit)
	 local humanoid = hit.Parent:FindFirstChild("Humanoid")
	if humanoid ~= nil and enabled == true then
		local enabled = false
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		local points = player.leaderstats.Points
		points.Value = points.Value + 5
		wait(3)
		enabled = true

	end
	
	
end)

So recently I'm learning how to code and I stumbled upon an error while coding a touched function. I tried debugging the error, but couldn't simply fix it. So I basically want to give my player five points each time the player touches an object. However, the script keeps on giving me way more than five points per touch. I added a de-bounce, but the error still occurs. Is there a way I can fix this?

2 comments

0

please make this easier to read

0

on line 12 try adding script.Parent.Touched:Disconnect()

1 answer

This is because you're not implementing your debounce correctly

Notice how I removed the local from enabled = false. You were creating a new enabled variable inside this event namespace.

local enabled = true

script.Parent.Touched:Connect(function(hit)
     local humanoid = hit.Parent:FindFirstChild("Humanoid")
    if humanoid ~= nil and enabled == true then
        enabled = false
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        local points = player.leaderstats.Points
        points.Value = points.Value + 5
        wait(3)
        enabled = true

    end


end)

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