Saving leaderstats and kill player for money script

Hi, I’m Shark.

I need a leaderstats script that saves, and also when you kill a player, you get money. I’ve tried it, but it just didn’t work, and for some reason I can’t get it to work.

Please, please, please, I really need help with this.

Sorry if I’ve made a mistake by posting this, but please help, I would really appreciate it.

We need your scripts in order to help you. Also any errors in the output?

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    
	
	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)
1 Like