When i rejoin after my “Money” and “Level” has saved, it dosen’t load in the data. I can’t seem to find the problem.
local DS = game:GetService("DataStoreService")
local moneyStore = DS:GetDataStore("Money")
local levelStore = DS:GetDataStore("Level")
local remote = game:GetService("ReplicatedStorage").Remotes.GiveCurrency
game.Players.PlayerAdded:Connect(function(player)
local moneyValue
local levelValue
local success, err = pcall(function()
moneyValue = moneyStore:GetAsync("Player_"..player.UserId)
levelValue = levelStore:GetAsync("Player-"..player.UserId)
end)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local money = Instance.new("IntValue")
money.Name = "Money"
money.Parent = leaderstats
local level = Instance.new("IntValue")
level.Name = "Level"
level.Parent = leaderstats
if success then
money.Value = moneyValue
level.Value = levelValue
else
print("Failed to load data")
end
end)
local function save(player)
local success, err = pcall(function()
moneyStore:SetAsync("Player_"..player.UserId, player.leaderstats.Money.Value)
levelStore:SetAsync("Player_"..player.UserId, player.leaderstats.Level.Value)
end)
if success then
print("Saved data")
else
print("Failed to save data")
end
end
local function autosave()
while wait (30) do
for i, players in pairs(game:GetService("Players"):GetPlayers()) do
save(players)
end
end
end
remote.OnServerEvent:Connect(function(player, amount)
player.leaderstats.Money.Value += amount
end)
spawn (autosave)
game.Players.PlayerRemoving:Connect(function()
save()
end)