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