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