Hello, So I’m making a game and I was wondering how can I add so my kills stat would save. I already added it so I just need the save thingy. The problem is that a guy gave me this script and I don’t really understand it that much.
My code:
local DataStoreService = game:GetService(“DataStoreService”)
local CashDS = DataStoreService:GetDataStore(“Cash”)
local RS = game:GetService(“RunService”)
game.Players.PlayerAdded:Connect(function(plr)
local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Parent = plr
Cash.Value = 0
local Kills = Instance.new("IntValue")
Kills.Name = "Kills"
Kills.Parent = plr
Kills.Value = 0
local data
local g, err = pcall(function()
data = CashDS:GetAsync("cash_"..plr.UserId)
end)
if not g then
warn(err)
else
if data ~= nil then
Cash.Value = data
end
end
end)
game:BindToClose(function()
if RS:IsStudio() then return end
local plrs = game.Players:GetPlayers()
for _, plr in pairs(plrs) do
local data = plr.Cash.Value
if plr:FindFirstChild("TagValue") then
data -= 50
end
if data then
local g, err = pcall(function()
CashDS:UpdateAsync("cash_"..plr.UserId, function(old)
local new = old or 0
new = data
return new
end)
end)
if not g then
warn(err)
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local data = plr.Cash.Value
if plr:FindFirstChild("TagValue") then
data -= 50
end
if data then
local g, err = pcall(function()
CashDS:UpdateAsync("cash_"..plr.UserId, function(old)
local new = old or 0
new = data
return new
end)
end)
if not g then
warn(err)
end
end
end)
local DataStoreService = game:GetService(“DataStoreService”)
local CashDS = DataStoreService:GetDataStore(“Cash”)
local RS = game:GetService(“RunService”)
game.Players.PlayerAdded:Connect(function(plr)
local Cash = Instance.new("IntValue")
Cash.Name = "Cash"
Cash.Parent = plr
Cash.Value = 0
local Kills = Instance.new("IntValue")
Kills.Name = "Kills"
Kills.Parent = plr
Kills.Value = 0
local data
local g, err = pcall(function()
data = {Cash = CashDS:GetAsync("cash_"..plr.UserId), Kills = CashDS:GetAsync("kills_"..plr.UserId)}
end)
if not g then
warn(err)
else
if data ~= nil then
Cash.Value = data.Cash
Kills.Value = data.Kills
end
end
plr.CharacterAdded:connect(function(Character)
local Humanoid = Character:FindFirstChild("Humanoid")
if Humanoid then
Humanoid.Died:Connect(function()
for i, Child in pairs(Humanoid:GetChildren()) do
if Child:IsA("ObjectValue") and Child.Value and Child.Value:IsA("Player") then
local Killer = Child.Value
if Killer:FindFirstChild("Kills") then
local Kills = Killer.Kills
Kills.Value = Kills.Value + 1
end
end
end
end)
end
end)
end)
game:BindToClose(function()
if RS:IsStudio() then return end
local plrs = game.Players:GetPlayers()
for _, plr in pairs(plrs) do
local data = {Cash = plr.Cash.Value, Kills = plr.Kills.Value}
if plr:FindFirstChild("TagValue") then
data.Cash -= 50
end
if data then
local g, err = pcall(function()
CashDS:UpdateAsync("cash_"..plr.UserId, function(old)
local new = old or 0
new = data.Cash
return new
end)
CashDS:UpdateAsync("kills_"..plr.UserId, function(old)
local new = old or 0
new = data.Kills
return new
end)
end)
if not g then
warn(err)
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local data = {Cash = plr.Cash.Value, Kills = plr.Kills.Value}
if plr:FindFirstChild("TagValue") then
data.Cash -= 50
end
if data then
local g, err = pcall(function()
CashDS:UpdateAsync("cash_"..plr.UserId, function(old)
local new = old or 0
new = data.Cash
return new
end)
CashDS:UpdateAsync("kills_"..plr.UserId, function(old)
local new = old or 0
new = data.Kills
return new
end)
end)
if not g then
warn(err)
end
end
end)