game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local kills = Instance.new("NumberValue", leaderstats)
kills.Name = "Kills"
local deaths = Instance.new("NumberValue", leaderstats)
deaths.Name = "Deaths"
player.CharacterAdded:Connect(function(character)
local Humanoid = character:FindFirstChild("Humanoid")
Humanoid.Died:Connect(function(died)
deaths.Value = deaths.Value + 1
local tag = Humanoid:FindFirsChild("creator")
local killer = tag.Value
if tag and killer then
killer.leaderstats:FindFirstChild("kills").Value = killer.leaderstats:FindFirstChild("kills").Value + 1
end
end)
end)
end)
The script is a server script inside of ServerScriptService.
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Parent = player
leaderstats.Name = "leaderstats"
local kills = Instance.new("NumberValue")
kills.Name = "Kills"
kills.Parent = leaderstats
local deaths = Instance.new("NumberValue")
deaths.Name = "Deaths"
deaths.Parent = leaderstats
player.CharacterAdded:Connect(function(character)
local Humanoid = character:FindFirstChild("Humanoid")
Humanoid.Died:Connect(function(died)
deaths.Value = deaths.Value + 1
local tag = Humanoid:FindFirstChild("creator")
local killer = tag.Value
if tag and killer then
killer.leaderstats:FindFirstChild("kills").Value = killer.leaderstats:FindFirstChild("kills").Value + 1
end
end)
end)
end)
When creating an instance through a call to the function “Instance.new()” you should parent the created instance after the instance has been created. This wasn’t the main issue however, the main issue occurs here.
An easier way to add, subtract multiply divide, etc is to do += this saves a lot of time and doesn’t require you to type the same thing and add a number
Problem is this line.
What is the ClassName for the creator tag? Is it a StringValue? Or something? Because if it is a string, then you need to retrieve their player instance through Players[tag.Value].
You’re making a mistake. The killer variable is the value of the tag inside the Humanoid. What you need to do is replace the local killer = tag.Value with local killer = game.Players:FindFirstChild(tag.Value)
The reason why this doesn’t work is because it only detects if the player dies the moment the player joins. This means that the script will activate once the player joins and since the player hasn’t died yet, the script will not activate and the code will finish running.
A solution to this would be to delete everything in the event function after the leaderstats stuff and put a script in playerscripts or characterscripts instead.
Not how Roblox’s events work. They call every time that the event happened so if the player died at any time the function will call. Also it register the event each time the character respawns.
Not true. They run every time the player dies. Same as what @IA_Restart said. They call every time that the event happened so if the player died at any time the function will call.
I have fixed everything you all have helped me with and it still doesn’t work. This error comes up: ServerScriptService.leaderstats:17: attempt to index nil with 'Value'
Here is my new script:
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder", player)
leaderstats.Name = "leaderstats"
local kills = Instance.new("NumberValue", leaderstats)
kills.Name = "Kills"
local deaths = Instance.new("NumberValue", leaderstats)
deaths.Name = "Deaths"
player.CharacterAdded:Connect(function(character)
local Humanoid = character:FindFirstChild("Humanoid")
Humanoid.Died:Connect(function(died)
deaths.Value = deaths.Value + 1
local tag = Humanoid:FindFirstChild("creator")
local killer = game.Players:FindFirstChild(tag.Value)
if tag and killer then
killer.leaderstats:FindFirstChild("Kills").Value = killer.leaderstats:FindFirstChild("Kills").Value + 1
end
end)
end)
end)