I moved everything to the server, however it is still erring on save and load.
Current script:
local dss = game:GetService("DataStoreService")
local store = dss:GetDataStore("PlayerData")
game.Players.PlayerAdded:Connect(function(player)
local success, err = pcall(function()
local key = "userkey:" ..player.UserId
local dataTable = store:GetAsync(key)
local coins, level = table.unpack(dataTable)
player.HiddenStats.Coins.Value = coins
player.HiddenStats.Level.Value = level
end)
if success then
warn("Loaded data!")
elseif err then
warn("Did not load data.")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, err = pcall(function()
local key = "userkey:" ..player.UserId
local data = {
player.HiddenStats.Coins.Value,
player.HiddenStats.Level.Value
}
store:SetAsync(key, data)
end)
if success then
warn("Saved data!")
elseif err then
warn("Did not save data.")
end
end)
game:BindToClose(function(player)
local success, err = pcall(function()
local key = "userkey:" ..player.UserId
local data = {
player.HiddenStats.Coins.Value,
player.HiddenStats.Level.Value
}
store:SetAsync(key, data)
end)
if success then
warn("Saved data!")
elseif err then
warn("Did not save data.")
end
end)
Ok, I was finally able to test your code, do you have Enable studio access to API Services enabled? Also, your parameter for the pcall is played. Delete that parameter and you should be good because this is overriding the player.UserId.
Yes sorry i meant player. Take out that parameter and it should work. Also if you want to print the error message all you have to do is use the variable for the error message inside a print.
I was reading another topic where someone was having trouble, and I think the problem might be that in studio it will save on game:BindToClose() because the studio server closes when you stop. The problem might be that I am doing game:BindToClose(player) when there is no player parameter, but rather it’s a for loop (this might be confusing I am typing this reply fast)
The post that helped me discover this
Also, I already have the player parameter in the pcall removed.
local runService = game:GetService("RunService")
game:BindToClose(function()
local players = game.Players:GetPlayers()
local saved = 0
for _, player in ipairs(players) do
coroutine.wrap(function()
local sucess = false
local tries = 0
repeat
local s, x = pcall(function()
local data = {
player.HiddenStats.Coins.Value,
player.HiddenStats.Level.Value
}
store:SetAsync("userkey:"..player.UserId, data)
end)
if s then sucess = true break else tries += 1 wait(6) end
until sucess or tries >= 3
saved += 1
end)()
end
repeat
runService.Heartbeat:Wait()
until saved >= #players
end)