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