Need help with a kill for money and saving leaderstats script

Hey there, I’m Shark.
I have a leaderstats that saves, but I want to know how to add a kill for money script to it.

I’ve tried, but it doesn’t work, so I need help on how to do it.
Here’s the script :

local DS = game:GetService("DataStoreService")
local moneyStore = DS:GetDataStore("Coins")
local remote = game:GetService("ReplicatedStorage").Remotes.GiveCurrency

game.Players.PlayerAdded:Connect(function(player)
	
	local moneyValue
	local success, err = pcall(function()
		moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
	end)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Parent = leaderstats	
	
	if success then
		money.Value = moneyValue
	else
		print("Failed to load data")
	end
end)

local function save(player)
	local success, err = pcall(function()
		moneyStore:SetAsync("Player_"..player.UserId, player.leaderstats.Money.Value)
	end)
	if success then
		print("Saved data")
	else
		print("Failed to load data")
	end
end

local function autosave()
	while wait(5) do
		for i, player in pairs(game:GetService("Players"):GetPlayers()) do
			save(player)
			end
		end
end

remote.OnServerEvent:Connect(function(player, amount)
	player.leaderstats.Money.Value += amount
		end)

spawn(autosave)

^^This is the script for the saving leaderstats^^

The following script is the kill for money script.
Here it is :


 player.CharacterAdded:connect(function(character)
  character:WaitForChild("Humanoid").Died:connect(function()
  local tag = character.Humanoid:FindFirstChild("creator")
   if tag ~= nil then
    if tag.Value ~= nil then
     money.Value = money.Value + 1
    end
   end
  end)
 end)
end)

So, I don’t know where to add the kill for money part into the script, and some help would be nice.

I basically need somewhere to add that part to the script, so when a player kills someone, they get money, and the money saves.

Thanks.

1 Like
Unrelated to the OP but still should be pointed out:

You’re calling SetAsync way too frequently, which would result in a lot of DataStore queue entries to be called on

I’d recommend increasing the time for the autosave() to happen often

You could just encase the CharacterAdded event inside your PlayerAdded event so that it’ll also detect when to give the stats when someone dies:

game.Players.PlayerAdded:Connect(function(player)
	
	local moneyValue
	local success, err = pcall(function()
		moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
	end)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Parent = leaderstats	
	
	if success then
		money.Value = moneyValue
	else
		print("Failed to load data")
	end
    
    player.CharacterAdded:Connect(function(character)
        local Humanoid = Character:WaitForChild("Humanoid")

        Humanoid.Died:Connect(function()
            local tag = Humanoid:FindFirstChild("creator")
            if tag and tag.Value then
                money.Value += 1
            end
        end)
    end)
end)

Could you possibly send me the whole script, because there seems to be errors appearing, and I don’t know why, if you haven’t noticed already, I’m not the greatest at scripting.

local DS = game:GetService("DataStoreService")
local moneyStore = DS:GetDataStore("Coins")

local remote = game:GetService("ReplicatedStorage").Remotes.GiveCurrency

game.Players.PlayerAdded:Connect(function(player)
	
	local moneyValue
	local success, err = pcall(function()
		moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
	end)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Parent = leaderstats	
end)


local function save(player)
	local success, err = pcall(function()
		moneyStore:SetAsync("Player_"..player.UserId, player.leaderstats.Money.Value)
	end)
	if success then
		print("Saved data")
	else
		print("Failed to load data")
	end
end

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

player.CharacterAdded:Connect(function(character)
	local Humanoid = character:WaitForChild("Humanoid")
		local money = Instance.new("IntValue")
	Humanoid.Died:Connect(function()
		local tag = Humanoid:FindFirstChild("creator")
		if tag and tag.Value then
			money.Value += 1
		end
	end)
end)
end)


local function autosave()
	while wait(5) do
		for i, player in pairs(game:GetService("Players"):GetPlayers()) do
			save(player)
			end
		end
end

remote.OnServerEvent:Connect(function(player, amount)
	player.leaderstats.Money.Value += amount
		end)

spawn(autosave)

Managed to do this, although I’m not sure if it would work…

I think I forgot something whoops

It should just be this:

local DS = game:GetService("DataStoreService")
local moneyStore = DS:GetDataStore("Coins")
local remote = game:GetService("ReplicatedStorage").Remotes.GiveCurrency

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

	local moneyValue
	local success, err = pcall(function()
		moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
	end)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Parent = leaderstats	

	if success then
		money.Value = moneyValue
	else
		print("Failed to load data")
	end    
	
	player.CharacterAdded:Connect(function(character)
		local Humanoid = character:WaitForChild("Humanoid")

		Humanoid.Died:Connect(function()
			local tag = Humanoid:FindFirstChild("creator")
			if tag and tag.Value then
				local killerstats = tag.Value:FindFirstChild("leaderstats")
				if killerstats then
					killerstats.money.Value += 1
				end
			end
		end)
	end)
end)

local function save(player)
	local success, err = pcall(function()
		moneyStore:SetAsync("Player_"..player.UserId, player.leaderstats.Money.Value)
	end)
	if success then
		print("Saved data")
	else
		print("Failed to load data")
	end
end

local function autosave()
	while wait(5) do
		for i, player in pairs(game:GetService("Players"):GetPlayers()) do
			save(player)
		end
	end
end

remote.OnServerEvent:Connect(function(player, amount)
	player.leaderstats.Money.Value += amount
end)

spawn(autosave)

Alright, let me see if it works.

It doesn’t seem to be working.

When I kill another play, my money doesn’t go up.

What is your creator value then? An ObjectValue?

I don’t know if this makes sense, but I want it to be a player, so when you kill another player, you’ll get 1 coin.

Wait I see the issue

local DS = game:GetService("DataStoreService")
local moneyStore = DS:GetDataStore("Coins")
local remote = game:GetService("ReplicatedStorage").Remotes.GiveCurrency

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

	local moneyValue
	local success, err = pcall(function()
		moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
	end)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Parent = leaderstats	

	if success then
		money.Value = moneyValue
	else
		print("Failed to load data")
	end    
	
	player.CharacterAdded:Connect(function(character)
		local Humanoid = character:WaitForChild("Humanoid")

		Humanoid.Died:Connect(function()
            print("OOF")
			local tag = Humanoid:FindFirstChild("creator")
			if tag and tag.Value then
				local killerstats = tag.Value:FindFirstChild("leaderstats")
				if killerstats then
                    print("This should work")
					killerstats.Money.Value += 1
				end
			end
		end)
	end)
end)

local function save(player)
	local success, err = pcall(function()
		moneyStore:SetAsync("Player_"..player.UserId, player.leaderstats.Money.Value)
	end)
	if success then
		print("Saved data")
	else
		print("Failed to load data")
	end
end

local function autosave()
	while wait(5) do
		for i, player in pairs(game:GetService("Players"):GetPlayers()) do
			save(player)
		end
	end
end

remote.OnServerEvent:Connect(function(player, amount)
	player.leaderstats.Money.Value += amount
end)

spawn(autosave)

Try this

Yes, this works, but there’s another problem, the person who kills me gets the money, I need it the other way around, if I kill someone, I get the money, also it doesn’t save…

Bruh that’s definitely not confusing at all aaa

local DS = game:GetService("DataStoreService")
local moneyStore = DS:GetDataStore("Coins")
local remote = game:GetService("ReplicatedStorage").Remotes.GiveCurrency

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

	local moneyValue
	local success, err = pcall(function()
		moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
	end)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Parent = leaderstats	

	if success then
		money.Value = moneyValue
	else
		print("Failed to load data")
	end    
	
	player.CharacterAdded:Connect(function(character)
		local Humanoid = character:WaitForChild("Humanoid")

		Humanoid.Died:Connect(function()
            print("OOF")
			local tag = Humanoid:FindFirstChild("creator")
			if tag and tag.Value then
				money.Value += 1
			end
		end)
	end)
end)

local function save(player)
	local success, err = pcall(function()
		moneyStore:SetAsync("Player_"..player.UserId, player.leaderstats.Money.Value)
	end)
	if success then
		print("Saved data")
	else
		print("Failed to load data")
	end
end

local function autosave()
	while wait(60) do
		for i, player in pairs(game:GetService("Players"):GetPlayers()) do
			save(player)
		end
	end
end

remote.OnServerEvent:Connect(function(player, amount)
	player.leaderstats.Money.Value += amount
end)

spawn(autosave)

I wouldn’t rely on saving the data every time the Player’s leaderstat changes, as that’ll result in a ton of DataStore queue entries

Same as the last time, so basically, this script is in ServerScriptService, and it everytime someone kills me, I get the points and also, it still doesn’t save.

I still don’t exactly know what you mean

The creator tag will work when the Player who killed someone will give them the stats, if you’re wanting to only make yourself get the stats just implement another conditional check if the Player object’s Name is equal to your name

How do I do that?
I’m not the greatest at scripting, and I really need help with this.

It still depends what how you’re wanting to implement this, if you only want yourself to gain the Money stats just add a check if the Humanoid that Died is equal to the Owner, then do this:

local DS = game:GetService("DataStoreService")
local moneyStore = DS:GetDataStore("Coins")
local remote = game:GetService("ReplicatedStorage").Remotes.GiveCurrency

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

	local moneyValue
	local success, err = pcall(function()
		moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
	end)
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player

	local money = Instance.new("IntValue")
	money.Name = "Money"
	money.Parent = leaderstats	

	if success then
		money.Value = moneyValue
	else
		print("Failed to load data")
	end    
	
	player.CharacterAdded:Connect(function(character)
		local Humanoid = character:WaitForChild("Humanoid")

		Humanoid.Died:Connect(function()
            print("OOF")
            if Character.Name == "ii_sharkboy" then
			    local tag = Humanoid:FindFirstChild("creator")
			    if tag and tag.Value then
                    save(player)
				    money.Value += 1
			    end
            end
		end)
	end)
end)

local function save(player)
	local success, err = pcall(function()
		moneyStore:SetAsync("Player_"..player.UserId, player.leaderstats.Money.Value)
	end)
	if success then
		print("Saved data")
	else
		print("Failed to load data")
	end
end

local function autosave()
	while wait(60) do
		for i, player in pairs(game:GetService("Players"):GetPlayers()) do
			save(player)
		end
	end
end

remote.OnServerEvent:Connect(function(player, amount)
	player.leaderstats.Money.Value += amount
end)

spawn(autosave)
1 Like

No, I want it like any player, so if anyone kills someone, that person will get money.

He wants it so when someone kills anyone they get money for it. For example, I killed you so now I get +1 coin.


I think something like this scripting tutorial would help you achieve what you need.
Some random script I found(it might error I didn’t check it):

game.Players.PlayerAdded:connect(function(player)
 local folder = Instance.new("Folder",player)
 folder.Name = "leaderstats"
 local currency1 = Instance.new("IntValue",folder)
 currency1.Name = "Cash"
 player.CharacterAdded:connect(function(character)
  character:WaitForChild("Humanoid").Died:connect(function()
  local tag = character.Humanoid:FindFirstChild("creator")
   if tag ~= nil then
    if tag.Value ~= nil then
     currency1.Value = currency1.Value + 10 --This is the reward after the player died.
    end
   end
  end)
 end)
end)

It’s all good, thank you guys!