Help! | Datastore, KillLeaderBoard

Hello!
I’m trying to store the number of kills a player has (Globally)!
Using Datastore
How would I detect when a player kills another player?
I know using something like

local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")

humanoid.Died:Connect(function()
	-- Do something!
end)

Would work, but I’m still kinda confused on this topic.

Kind Regards Bugle, (If this Post breaks TOS please alert me!)

Most tools use a “creator” value under the humanoid of the character to tell who killed the player. BUT, if you use custom tools or a tool that doesn’t create the “creator” value, you can just add the kills from the damaging part of the script. With a tool that creates the “creator” value:

game.Players.PlayerAdded:Connect(function(plr)
	plr.CharacterAdded:Connect(function(char)
		char:WaitForChild("Humanoid").Died:Connect(function()
			if char.Humanoid:FindFirstChild("creator") then
				char.Humanoid.creator.Value.leaderstats.Kills.Value += 1
			end
		end)
	end)
end)
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Stats")



game.Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"
	local Money = Instance.new("IntValue", Leaderstats)
	Money.Name = "Money"
	Money.Value = 0
	
	spawn(function()
		while wait(1) do
			Money.Value = Money.Value + math.random(1,5)
		end
	end)
	
	local Data = DataStore:GetAsync(Player.UserId)
	if Data then
		Money.Value = Data
	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, Player.leaderstats.Money.Value)
end)

Yea, this is what i have for my money script (This was a test script)

Well, you will need to first create the “Kills” value. Also, it is a bad practice to set the parent of the objects before changing their properties.

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Stats")



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

	local Data = DataStore:GetAsync(Player.UserId) or {}

	if type(Data) == "number" then
		Data = {"Money" = Data}
	end

	local Money = Instance.new("NumberValue")
	Money.Name = "Money"
	Money.Value = Data.Money or 0
	Money.Parent = Leaderstats

	local Kills = Instance.new("NumberValue")
	Kills.Name = "Kills"
	Kills.Value = Data.Kills or 0
	Kills.Parent = Leaderstats
	
	spawn(function()
		while wait(1) do
			Money.Value = Money.Value + math.random(1,5)
		end
	end)
end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, {"Money" = Player.leaderstats.Money.Value, "Kills" = Player.leaderstats.Kills.Value})
end)
1 Like

Good to know, I knew it was bad practice but I really was not thinking at the time of scripting the example script.
It was 4am.
Thanks for the help!

1 Like

Hey so like @Kaid3n22 said, you need to create a creator tag in the player that dieds humanoid, this creator tag, should be of class ObjectValue the value of this should be the player that killed them. Then when someone dies check their humanoid for a creator tag, get the killer (which is creatorTag.Value) and add a kill to their leaderstats. Something I think could help you is my YouTube video on making a kill feed (now it doesn’t have data stores, but the data store is the same as any other) because we use creator tags the same way:

BTW @NotBugle, the script you marked as solution gives you the leaderstats, however,

It doesn’t give this, have you figured it out. If not read the rest of this post (if you havent already)

(After Publish Problems)

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Stats")

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

	local Data = DataStore:GetAsync(Player.UserId) or {}

	local Kills = Instance.new("NumberValue")
	Kills.Name = "Kills"
	Kills.Value = Data.Kills or 0
	Kills.Parent = Leaderstats

end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, Player.leaderstats.Kills.Value)
end)

Is my script I removed money so i can fix it and throw it in at a later date but now I’m getting (attempt to index number with ‘Kills’)

Change that to just Data

So:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("Stats")

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

	local Data = DataStore:GetAsync(Player.UserId) or 0

	local Kills = Instance.new("NumberValue")
	Kills.Name = "Kills"
	Kills.Value = Data or 0
	Kills.Parent = Leaderstats

end)

game.Players.PlayerRemoving:Connect(function(Player)
	DataStore:SetAsync(Player.UserId, Player.leaderstats.Kills.Value)
end)

Sometimes I wonder, how i manage to screw stuff up like this…
Thanks!

No, this wasn’t on you. Data used to be a table, because it had more then one value (money and kills), so to get the kills you would Data.Kills and for money Data.Money, however, now that money is removed, it is just an number because you saved it as a number, and not a table

Notice, how you saved it as a dictionary here.

But not here.
Another way to fix that error would have been changing the quote above to
{"Kills" = Player.leaderstats.Kills.Value}

1 Like

Im very confused now, my In-Game LeaderBoard is not updating
image

(No Error)

Oh, that was my fault :sweat_smile: I had assumed he wanted to keep the “Money” too and not just the “Kills,” so I adapted to that.

Thanks for you help i got everything working!

Everything is solved thanks for the help!