Need help with stats script

Hey, I’m Shark.

I need help with this script.

So basically, I have this script, and I need help with 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 = "Coins"
	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.Coins.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.Coins.Value += amount
end)

spawn(autosave)

The script is a leaderstats script that is supposed to save, and also, when you kill a player, you’re supposed to get money.

The problem is, when I kill a player, the player that I KILL gets the money, I need it the opposite way around, so that I GET THE MONEY when I KILL someone.

Also, I want to know if this script will save the leaderstats.

and the kill for money part of the script, it’s supposed to be if any player kills another play, they get the money.

I need help with this, so any help would be appreciated.

Thank you!

You probably need to change the Died event to this

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

Assuming the thing stored i nthe creator tag is a player instance

Ok, here is the problem:

It gives a point when the player dies, not when you kill the player.

Could you provide me with the full script, if possible, because it keeps saying expected end or something, and I’m not sure where to put it.

Replace the Died event with the code I gave



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 = "Coins"
	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
				tag.Value.leaderstats.Coins.Value += 1
			end
		end)
	end)
end)

local function save(player)
	local success, err = pcall(function()
		moneyStore:SetAsync("Player_"..player.UserId, player.leaderstats.Coins.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.Coins.Value += amount
end)

spawn(autosave)

Yes! It works, thank you so much!!!

1 Like