Ok so I had leaderstats already but, I was trying to get a data save too but when I changed script data save nor leaderstats were there. what’s wrong?
dss = game:GetService(“DataStoreService”)
local DataStoreToKeep = dss:GetDataStore(“TypeWhatYouWanthere”)
game.Players.PlayerAdded:Connect(function(player)
local is = Instance.new(“Folder”,player)
is.Name = “leaderstats”
local money = Instance.new(“IntValue”,is)
money.Name = “Money”
money.Value = 0
local data
local success, errormessage = pcall(function()
data = DataStoreToKeep:GetAsync(player.UserId…"~money")
end)
if success then
money.Value = data
else
warn(errormessage)
end
end)
local success, errormessage = pcall(function()
DataStoreToKeep:GetAsync(player.UserId…"~money",player.leaderstats.Money.Value)
end)
if success then
else
warn(errormessage)
end
end)
Try adding a :BindToClose() function at then end of your script:
game:BindToClose(function()
for i, player in pairs(game.Players:GetPlayers()) do
DataStoreToKeep:SetAsync(player.UserId.."~money", player.leaderstats.Money.Value)
end
end)
You made a small typo . In the Players.PlayerRemoving event function you want to set the data using :SetAsync() not :GetAsync(). No worries. I get typing mistakes all the time! Remember to post this as your solution if it works!
game.Players.PlayerRemoving:Connect(function(player)
local success, errormessage = pcall(function()
DataStoreToKeep:SetAsync(player.UserId.."~money",player.leaderstats.Money.Value)
end)
if success then
else
warn(errormessage)
end
end)
game:BindToClose(function()
for i, player in pairs(game.Players:GetPlayers()) do
DataStoreToKeep:SetAsync(player.UserId.."~money", player.leaderstats.Money.Value)
end
end)
This isn’t a free model code… I watched a tutorial learning how to script, I’m not relying on anything… I’m just tired of working on a script that’s not working for 3 hours straight.