Function repeats even with debounce

The print signifies that the character is loading in. I think this could be going two ways:

  1. The function repeats for each part of the character loaded in.
  2. The function repeats because of _, v in pairs even though there is 1 player.
while wait(0.4) do
	for _,v in pairs(players:GetPlayers()) do
		local spawndebounce = true
		v.CharacterAdded:Connect(function()
			if spawndebounce == true then
				v.leaderstats.Deaths.Value += 1
				print('overstack')
				respawnflash:FireClient(v)
				spawndebounce = false
				wait(1)
				spawndebounce = true
			end
		end)
	end
end

image

4 Likes

it’s normal because you have started by making the variable true, so it will execute it , and on the rest of the script you set it to false then back to true so it will be looping it non stop

4 Likes

How would i work around that then im stumped.

1 Like

first tell what are you trying to do, then what have you tried already and if you can give the rest of the script it would help

2 Likes

I guess it will work.

local spawndebounce = true
while wait(0.4) do
	for _,v in pairs(players:GetPlayers()) do
		v.CharacterAdded:Connect(function()
			if spawndebounce == true then
				v.leaderstats.Deaths.Value += 1
				print('overstack')
				respawnflash:FireClient(v)
				spawndebounce = false
				wait(1)
				spawndebounce = true
			end
		end)
	end
end
3 Likes

this is a server script i want the game to update a leaderstat of deaths whenever someone dies putting this outside of the loop would debounce all players

1 Like

Hmm… Then try this:

local DebouncedPlayers = {}
while wait(0.4) do
	for _,v in pairs(players:GetPlayers()) do
		v.CharacterAdded:Connect(function()
			if not DebouncedPlayers[v.Name] then
				v.leaderstats.Deaths.Value += 1
				print('overstack')
				respawnflash:FireClient(v)
				DebouncedPlayers[v.Name] = v
				wait(1)
                DebouncedPlayers[v.Name] = nil
			end
		end)
	end
end
3 Likes

Oh i should’ve thought of making list! Thank you so much.

1 Like

It works thank you so much I was really stuck

1 Like

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