I need help for my script to get cash per kill

Hello, I made a script (with some tutorials) for my game with a leaderstats with Coins and Kills. It got a datastore with to save the player stats.

So I need to add something in the script to give player Coins when he kill someone, so in his leaderstats he got +20coins and +1kill.

Im really bad at scripting so if someone can change or tell me what to add to my current script to make it possible :smiley:

Here my script :

local DataStoreService = game:GetService('DataStoreService')
local playerData = DataStoreService:GetDataStore('PlayerData')

local function onPlayerJoin(player) 
	local leaderstats = Instance.new('Folder')  

	leaderstats.Name = 'leaderstats'

	leaderstats.Parent = player



	local coins = Instance.new('IntValue') 

	coins.Name = 'Coins'

	coins.Parent = leaderstats
	
	local kills = Instance.new('IntValue')
	kills.Name = 'Kills'
	kills.Parent = leaderstats
	
	local playerUserId = 'Player_'..player.UserId
	local data = playerData:GetAsync(playerUserId)
	if data then
		coins.Value = data['Coins']
		kills.Value = data['Kills']
	else
		coins.Value = 0
		kills.Value = 0
		
	end
end


local function create_table (player)
	local player_stats = {}
	for _, stat in pairs(player.leaderstats:GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end
	return player_stats
end

local function onPlayerExit(player)
	local player_stats = create_table(player)
	local success, err = pcall(function()
		local playerUserId = 'Player_'..player.UserId
		playerData:SetAsync(playerUserId, player_stats)
	end)
	
	if not success then
		warn ('Could not save data')
	end
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

Thank you for helping me :slight_smile:

You could create a module script specifically for accessing/modifying player data and when the server registers a kill from the player it will call a function from the module script to add the kill and cash values to the given player’s leaderstat stats

Another way is to make an array for leaderstats and then go through a for loop to save & load them.

You should add a CloseToBind:() function.

game:CloseToBind(function()
       wait(3)
end)

This makes sure the data is being saved.

local Players = game:GetService("Players")
local TestService = game:GetService("TestService")
local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStoreValues") --Name the DataStore whatever you want

Players.PlayerAdded:Connect(function(player)

    local leaderstats = Instance.new('Folder')
    leaderstats.Name = 'leaderstats'
    leaderstats.Parent = player


	local Coins = Instance.new('IntValue')
	Coins.Name = 'Coins'
    Coins.Parent = leaderstats
	Coins.Value = 0

    local Kills = Instance.new('IntValue')
    Kills.Name = 'Kills'
    Kills.Parent = leaderstats
    Kills.Value = 0

	local value1Data = Coins
    local value2Data = Kills

player.CharacterAdded:Connect(function(Character)
		Character.Humanoid.Died:Connect(function(Died)
			local creator = Character.Humanoid:FindFirstChild("creator")
			local leaderstats = creator.Value:FindFirstChild("leaderstats")
			if creator ~= nil and creator.Value~= nil then
				leaderstats.Coins.Value += 20
                                leaderstats.Kills.Value += 1
			end
		end)
	end)

	local s, e = pcall(function()
		value1Data = DataStore:GetAsync(player.UserId..'-Value1') or 0 --check if they have data, if not it'll be "0"
        value2Data = DataStore:GetAsync(player.UserId..'-Value2') or 0
	end)

	if s then
		Coins.Value = value1Data --setting data if its success
        Kills.Value = value2Data
	else
		TestService:Error(e)  --if not success then we error it to the console
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
local s, e = pcall(function()
	DataStore:SetAsync(player.UserId..'-Value1', player.leaderstats.Coins.Value) --setting data
    DataStore:SetAsync(player.UserId..'-Value2', player.leaderstats.Kills.Value)
	end)
	if not s then TestService:Error(e) 
	end
end)

game:BindToClose(function(player)
      if not RunService:IsStudio()
          local s, e = pcall(function()
	      DataStore:SetAsync(player.UserId..'-Value1', player.leaderstats.Coins.Value) --setting data
          DataStore:SetAsync(player.UserId..'-Value2', player.leaderstats.Kills.Value)
	end)
	if not s then TestService:Error(e) 
	end

       end)
end)

I hope this helped. :slight_smile:

1 Like

Well, idk if it work or not, but there is an error when I launch it

Can you tell me what the error is labeled as? Then I’ll definitely be able to fix the script :slight_smile:

It say that in the output


And its the “local” at line 62.

Remove the end) on line 65

Its still making the error without the “end)”

image
it should be if not RunService:IsStudio() then

it should create an end then paste all the other code inside of that end

Of course! I’m being an idiot. Thanks.

Here’s the final script.

local Players = game:GetService("Players")
local TestService = game:GetService("TestService")
local RunService = game:GetService("RunService")
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStoreValues") --Name the DataStore whatever you want

Players.PlayerAdded:Connect(function(player)

    local leaderstats = Instance.new('Folder')
    leaderstats.Name = 'leaderstats'
    leaderstats.Parent = player


	local Coins = Instance.new('IntValue')
	Coins.Name = 'Coins'
    Coins.Parent = leaderstats
	Coins.Value = 0

    local Kills = Instance.new('IntValue')
    Kills.Name = 'Kills'
    Kills.Parent = leaderstats
    Kills.Value = 0

	local value1Data = Coins
    local value2Data = Kills

player.CharacterAdded:Connect(function(Character)
		Character.Humanoid.Died:Connect(function(Died)
			local creator = Character.Humanoid:FindFirstChild("creator")
			local leaderstats = creator.Value:FindFirstChild("leaderstats")
			if creator ~= nil and creator.Value~= nil then
				leaderstats.Coins.Value += 20
                                leaderstats.Kills.Value += 1
			end
		end)
	end)

	local s, e = pcall(function()
		value1Data = DataStore:GetAsync(player.UserId..'-Value1') or 0 --check if they have data, if not it'll be "0"
        value2Data = DataStore:GetAsync(player.UserId..'-Value2') or 0
	end)

	if s then
		Coins.Value = value1Data --setting data if its success
        Kills.Value = value2Data
	else
		TestService:Error(e)  --if not success then we error it to the console
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
local s, e = pcall(function()
	DataStore:SetAsync(player.UserId..'-Value1', player.leaderstats.Coins.Value) --setting data
    DataStore:SetAsync(player.UserId..'-Value2', player.leaderstats.Kills.Value)
	end)
	if not s then TestService:Error(e) 
	end
end)

game:BindToClose(function(player)
      if not RunService:IsStudio() then
          local s, e = pcall(function()
	      DataStore:SetAsync(player.UserId..'-Value1', player.leaderstats.Coins.Value) --setting data
          DataStore:SetAsync(player.UserId..'-Value2', player.leaderstats.Kills.Value)
	end)
	if not s then TestService:Error(e) 
	end

       end)
end)
1 Like

u forgot to put if not RunService:IsStudio() then

Thank you sooo much you life saver ! :smiley:

No problem. I’m glad I could help :slight_smile: