Debounce issues causing lives to go negative

So my script is supposed to detect when a player touches it, take away a life, and teleport them back to the start. If they have no lives left, it’s supposed to teleport them to the lobby. I think the issue has something to do with the debounce. When I touch it, my lives value goes to this: image

With some more testing, I found out that it works the second time I touch it, but not the first time.

local Debounce = true

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") ~= nil then
		if Debounce == true then
			local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
			Debounce = false
			Player.PlayerVariables.Lives.Value = Player.PlayerVariables.Lives.Value - 1
			
			if Player.PlayerVariables.Lives.Value >= 1 then
				local DestinationObj = game.Workspace.StartingIsland["Teleport Pad"].Teleport
				local Destination = CFrame.new(Vector3.new(DestinationObj.Position.X, DestinationObj.Position.Y, DestinationObj.Position.Z))
		
				Player.Character.HumanoidRootPart.Anchored = true
				Player.Character.HumanoidRootPart.CFrame = Destination
				Player.Character.HumanoidRootPart.Anchored = false
			else
				local DestinationObj = game.Workspace.Lobby["Teleport Pad"].Teleport
				local Destination = CFrame.new(Vector3.new(DestinationObj.Position.X, DestinationObj.Position.Y, DestinationObj.Position.Z))
		
				Player.Character.HumanoidRootPart.Anchored = true
				Player.Character.HumanoidRootPart.CFrame = Destination
				Player.Character.HumanoidRootPart.Anchored = false
			end
			
			wait(2.5)
			Debounce = true
		end
	end
end)

I didn’t easily see this, but the reason why it goes -1 is because that’s the first thing you do in your statement. For example let’s say I have 0 lives and I touch the part. Well, your statement will set it to -1. I’m not sure why it wouldn’t teleport them back to the lobby. Perhaps you can change it to > 0, but this probably may not fix it.

1 Like

It was originally > 0. As for the first thing it does making it set to -1, it is setting the lives value to the live values minus 1.
Player.PlayerVariables.Lives.Value = Player.PlayerVariables.Lives.Value - 1

Everyone starts with three hearts, or more if they have gamepasses. So I’m confused as to how it’s becoming -1 when its subtracting 1 from 3.

1 Like

If you wouldn’t want to have it set to -1 you can move that statement into the => 1 and change it to ~= 0

I figured out the issue. I can’t believe I was this stupid lol. I wasn’t waiting for the round script to set the lives value to 3, but instead moving my character and testing it when the value was 0. That’s why it worked for me after I tried the following times.

1 Like