Leaderstats wont go up, please help

So, everytime you die, I want the deaths value going up by 1. What did I do wrong here? I am a beginner so any help would be appreciated.

local DataStoreService = game:GetService("DataStoreService")

local DataStore = DataStoreService:GetDataStore("Deaths")

game.Players.PlayerAdded:Connect(function(player)

local leaderstats = Instance.new("Folder")

leaderstats.Name = "leaderstats"

leaderstats.Parent = player

local Deaths = Instance.new("StringValue")

Deaths.Name = "Deaths"

Deaths.Value = DataStore:GetAsync(player.UserId) or 0

Deaths.Parent = leaderstats

local character = player.character

character.Died:Connect(function() -- don't know what to do in this line

player.Deaths.Value = player.Deaths.Value + 1

end)

end)

game.Players.PlayerRemoving:Connect(function(Player)

DataStore:SetAsync(Player.UserId, Player.leaderstats.Deaths.Value)

end)

Try accessing the Humanoid when getting the “Died” event

character.Humanoid.Died:Connect(function()
-- do stuff when the character dies.
end)

1 Like

this comes up:

image

Do I need to define character again? or am I missing something here?

Hey erm, you said “player.Deaths.Value = player.Deaths.Value + 1” instead of their leaderstats?

1 Like

This would be the fix you’re looking for I think:

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

local DataStore = DataStoreService:GetDataStore("Deaths")


Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Deaths = Instance.new("StringValue")
	Deaths.Name = "Deaths"
	Deaths.Value = DataStore:GetAsync(player.UserId) or 0
	Deaths.Parent = leaderstats

	player.character.Humanoid.Died:Connect(function() -- don't know what to do in this line
		leaderstats.Deaths.Value = leaderstats.Deaths.Value + 1
	end)

end)

Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, Player.leaderstats.Deaths.Value)
end)
1 Like

That will probably work but the script doesn’t know if it is going up because it doesn’t know the player died.

this bit, it says “Attempt to index nil with ‘Humanoid’” in output.

You would need to wait until character and humanoid added, because scripts run first than character and humanoid are added.

So like this:

player.CharacterAdded:Connect(function(character)
  character:WaitForChild("Humanoid").Died:Connect(function()
    leaderstats.Deaths.Value = leaderstats.Deaths.Value + 1
  end)
end)
1 Like

Change it to

player.CharacterAdded:Connect(function(Character)
	Character:WaitForChild("Humanoid").Died:Connect(function() -- don't know what to do in this line
		leaderstats.Deaths.Value = leaderstats.Deaths.Value + 1
	end)
end)
1 Like

Yea you’re right, I completely forgot to do CharacterAdded, I’m focused on something else at the same time.

1 Like