Why does the score multiply?

Hey everyone! I successfully added a kill counter to a game I’m working on recently. It works, but not exactly as I wanted it to. When someone gets a kill, they are supposed to get 1 point. But, whenever they get a kill they get either 3 points or 5 points. I’m confused on this one, please help me.

player.CharacterAdded:Connect(function(Character)
					Character.Humanoid.Died:Connect(function(Died)
						local creator = Character.Humanoid:FindFirstChild("creator")
						local leaderstats = creator.Value:FindFirstChild("leaderstats")
						if creator ~= nil and creator.Value ~= nil then
							leaderstats.Points.Value = leaderstats.Points.Value + 1
						end
					end)
				end)

Side note: This script is part of a bigger script. If you need the whole script, just ask me.

Try adding debounce? I’m not sure because when an event is fired, a new thread will be created.

Like this?

player.CharacterAdded:Connect(function(Character)
					Character.Humanoid.Died:Connect(function(Died)
						local creator = Character.Humanoid:FindFirstChild("creator")
						local leaderstats = creator.Value:FindFirstChild("leaderstats")
						if creator ~= nil and creator.Value ~= nil then
							leaderstats.Points.Value = leaderstats.Points.Value + 1
                           debounce = false
						end
					end)
				end)

EDIT (removed :WaitForChild() )

EDIT 2 it’s the same code you provided, but I reduced wait time now.

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local debounce = false

local function onDeath(character)
	debounce = true
	local creator = character.humanoid:FindFirstChild("creator")
	local leaderstats = creator.Value:FindFirstChild("leaderstats")
	if (creator and creator.Value ~= nil) then
		leaderstats.Points.Value += 1
	end
	RunService.Heartbeat:Wait()
	debounce = false
end

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character.Humanoid.Died:Connect(function(died)
			if (not debounce) then onDeath(character) end
		end)
	end)
end)

This doesn’t work at all. It gives no points for each kill.

Add a debounce, excuse poor formatting I am on mobile at school.

player.CharacterAdded:Connect(function(Character)
local debounce = false Character.Humanoid.Died:Connect(function(Died)
if debounce = false then
debounce = true
local creator = Character.Humanoid:FindFirstChild(“creator”)
local leaderstats = creator.Value:FindFirstChild(“leaderstats”)
if creator ~= nil and creator.Value ~= nil then
leaderstats.Points.Value = leaderstats.Points.Value + 1

end
debounce = false
end
end)
end)

why not just disconnecting the event, I’m pretty sure if the event is originated inside the character added; it’s going to be re-connected if the character respawn.

local conn
conn = Humanoid.Died:Connect(function()
   conn:Disconnect()
   conn = nil
end)

full code:

player.CharacterAdded:Connect(function(character)
    local humanoid = character:WaitForChild("Humanoid")
    local conn
    conn = humanoid.Died:Connect(function()
        local creator = humanoid:FindFirstChild("creator")
        local leaderstats = creator and creator.Value:FindFirstChild("leaderstats")
        if leaderstats then
            leaderstats.Points.Value += 1
        end
        conn:Disconnect()
        conn = nil
    end)
end)
2 Likes

So this works, but it seems to be broken. Let’s say I get a kill. I would get 1 point. Next time I get a kill though, I would get 2 points. It keeps going up like that every time I get a kill. If I get a kill a third time, I would get 3 points. Do you know how to fix this?

EDIT @PorgDotOrg this is an individual script that belongs in ServerScriptService and creates leaderstats by itself. No other scripts with such functionality are now needed.

EDIT Try now.

@LiamKyd this is a good idea, so I implemented it here (thanks). By the time event is disconnected, player can get two, three, or even four hits. It is not a replacement for debounce, which is still very much needed. Disconnecting of events is also done automatically in a short period of time after character is destroyed and it’s parent is set to nil. From a performance aspect it doesn’t make a key difference, although it speeds up the process, so it’s a good practice. When character is deleted, player is assigned a new one, and old one eventually deletes, so connections don’t reconnect. That is also one of the reasons why we use player.CharacterAdded event. See the script below leaderstats script.

Leaderstats script

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local debounces = {}

Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local points = Instance.new("IntValue")
	points.Name = "Points"
	points.Parent = leaderstats

	player.CharacterAdded:Connect(function(character)
		debounces[character.Name] = false
		local humanoid = character:WaitForChild("Humanoid")
		local conn; conn = humanoid.Died:Connect(function()
			local creator = humanoid:FindFirstChild("creator")
			if (creator and not debounces[player.UserId]) then
				debounces[character.Name] = true
				local killer = creator.Value
				if killer then killer.leaderstats.Points.Value += 1 end
			end
			conn:Disconnect()
			conn = nil
		end)
	end)
end)

Why doesn’t .Died connection reconnect?

In the following scenario, we are reference the character one we join. The script finds it, registers it, and keeps printing it’s parent, which is normall workspace. Try resetting. After the character will be destroyed and a new one will have spawned, script will keep running in the memory and printing out the old character’s parent in the output, which will be nil by that time. The script also won’t stop running and character probably won’t get fully cleared from memory, because it is still referenced, so it can’t be collected by GC (garbage collector).

--[[
]]
local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	local character = player.Character or player.CharacterAdded:Wait()
	while (true) do
		print(character.Parent)
		wait(1)
	end
end)

What would be the right way? Waiting for new characters to spawn and load.

Try repeating the same process as above? After character respawns, we have two scripts printing out parents in the output. The new one and the old one that didn’t get deleted, because one of the scripts inside is still actively working and keeping the reference, thus preventing the memory clearning (these are so called momory leaks)

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		while (true) do
			print(character.Parent)
			wait(1)
		end
	end)
end)

@dukzae thank you too again for informing me about RunService.Heartbeat (its successor is now brand new not long ago introduced

RunService.PostSimulation

Thank you so much! I just need to save it, but that will be easy.

1 Like

After further testing, we found that this script does not work properly. Again, we get a random number of points every time we get a kill. I have no idea why.

It is not deprecated, in fact, the new re-named alternatives do not even fire yet. It’s incorrectly marked on the dev portal.

This is what I meant by reconnected also great explanation.

1 Like