Help regarding leaderstats and players

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I would like help and a script which detects when a player kills someone or the player dies, and adds one to the leaderstats.
  2. What is the issue? Include enough details if possible!
    I can’t figure out how to make it detect when a player dies and when they kill someone.
  3. What solutions have you thought of so far?
    I have tried seeing if humanoid health was less then 1 and if it was, it would add one to the deaths, but i dont know about the kills

All the leaderstats are done, and they are all being saved, I can also provide the script for the leaderstats if you need!

local datastore = game:GetService("DataStoreService"):GetDataStore("GameStore") 


game.Players.PlayerAdded:connect(function(player)
	local leaderstats = Instance.new("IntValue")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local points = Instance.new("IntValue")
	points.Name = "Level"
	points.Parent = leaderstats
	
	local wins = Instance.new("IntValue") 
	wins.Name = "Kills"
	wins.Parent = leaderstats
	
	local deaths = Instance.new("IntValue") 
	deaths.Name = "Deaths"
	deaths.Parent = leaderstats
	
	local key = "user-" .. player.userId
	
	local storeditems = datastore:GetAsync(key)
	if storeditems then
		points.Value = storeditems[1] 
		wins.Value = storeditems[2] 
		deaths.Value = storeditems[3]
	else
		local items = {points.Value, wins.Value, deaths.Value} 
		datastore:SetAsync(key, items)
	end
end)

game.Players.PlayerRemoving:connect(function(player)
	local items = {player.leaderstats.Points.Value, player.leaderstats.Wins.Value, player.leaderstats.Deaths.Value} 
	local key = "user-" .. player.userId
	
	datastore:SetAsync(key, items)
end)






this is the datastore script

2 Likes

This might help with detecting when the player is dead:
https://developer.roblox.com/en-us/api-reference/event/Humanoid/Died
And this might help with adding a kill to the player’s stats:

1 Like

Still doesn’t help me sorry. Could you explain a bit better on how to do it?

1 Like

I recommend researching more on DevForum, since you are not the first who asked about this.

This will give you a wider point of view on what to do about which player died and who killed them!