Leaderboard and debouncing problem

Hello Roblox developers, so the issue I’m having is when a player falls into the lava, a death count gets added to the leaderboard, but it gets triggered to many times. So what I did to resolve this issue is to add debouncing to my code… But now I’m having another issue when that same player touches that same part of lava, the new death count does not get uploaded to the leaderboard. if any knows how to fix this issue please let me know thanks! you can view my code below

local lava = script.Parent

local function killplayer(otherpart)
	local partparent = otherpart.Parent
	local humanoid = partparent:FindFirstChild("Humanoid")
	if humanoid then
		humanoid.Health = 0
	end
end

lava.Touched:Connect(killplayer)

local isTouched = false ----where the leaderboard code starts

local function touched(hit)
	if isTouched then
		return
	end
	local Player = game.Players:GetPlayerFromCharacter(hit.Parent)

	if Player then
		local Deaths = Player.leaderstats.Deaths
		Deaths.Value = Deaths.Value + 1
		isTouched = true
	end
end

lava.Touched:Connect(touched)
1 Like

Just wait for the Player’s CharacterAdded event, and reset the debounce there.

hate to ask, but how would that look? sorry I’m fairly new to scripting lol…

Fair enough, something like this:

local function touched(hit)
	if isTouched then return end -- Neater to put these in one line
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	
	if player then
		local deaths = player.leaderstats.Deaths
		deaths.Value += 1 -- Same as deaths.Value = deaths.Value + 1
		isTouched = true
		
		player.CharacterAdded:Once(function() -- :Once() is like :Connect(), but it only fires once.
			isTouched = false
		end)
	end
end

Also made some adjustments with comments to explain them. You’re welcome c:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.