What do you want to achieve? Keep it simple and clear!
I want the data to save properly as loading data returns as nil
What is the issue? Include screenshots / videos if possible!
Whenever data is attempted to load, data isnt a table, just nill.
players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local coins = Instance.new("IntValue")
coins.Name = "Coins"
coins.Parent = leaderstats
local weapons = Instance.new("Folder")
weapons.Name = "Weapons"
weapons.Parent = player
local PlayerUserId = "player_"..player.UserId
local Data
local success, err = pcall(function()
Data = ds:GetAsync(PlayerUserId)
end)
if success then
coins.Value = Data["coins"]
local weapons = Data["dataweapons"]
for i, v in pairs(weapons) do
v.Parent = player.Weapons
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local weapons = player.Weapons
local coins = player.leaderstats.Coins
local PlayerUserId = "player_"..player.UserId
local data = {}
local dataweapons = weapons:GetChildren()
data[weapons] = dataweapons
data[coins] = coins.Value
for i, v in pairs(data) do
print(v)
end
local success, err = pcall(function()
ds:SetAsync(PlayerUserId, data)
end)
if success then
print("data saved succesfully")
end
end)
If you’re testing in Solo, most likely the server is closing down before the data has a chance to actually save. Along with when the player leaves, you should also use game:BindToClose(func) and save data then too. The benefit here is that bound functions on that API will keep the server alive until the function is done (there might be a timeout at some point).
Another note: It might be better to save all your data in one key as a table of all your stats there, instead of a bunch of different keys.
By the way, the only error in the output is ServerScriptService.LAS:24: attempt to index nil with ‘coins’ and I tried printing everything out from data, but data isnt a table(what I want it to be)
I dont really know how to use bindtoclose but is this close?
local Leaving = function(player)
local weapons = player.Weapons
local coins = player.leaderstats.Coins
local PlayerUserId = "player_"..player.UserId
local data = {}
local dataweapons = weapons:GetChildren()
data[weapons] = dataweapons
data[coins] = coins.Value
for i, v in pairs(data) do
print(v)
end
local success, err = pcall(function()
ds:SetAsync(PlayerUserId, data)
end)
if success then
print("data saved succesfully")
end
end
game.Players.PlayerRemoving:Connect(function(player)
game:BindToClose(Leaving(player))
end)
game.Players.PlayerRemoving:Connect(function(player)
game:BindToClose(function()
for i, v in ipairs(game.Players:GetChildren()) do
local weapons = player.Weapons
local coins = player.leaderstats.Coins
local PlayerUserId = "player_"..player.UserId
local data = {}
local dataweapons = weapons:GetChildren()
data[weapons] = dataweapons
data[coins] = coins.Value
for i, v in pairs(data) do
print(v)
end
local success, err = pcall(function()
ds:SetAsync(PlayerUserId, data)
end)
if success then
print("data saved succesfully")
end
end
end)
end)
game:BindToClose(function()
for i, v in ipairs(game.Players:GetChildren()) do
local weapons = v.Weapons
local coins = v.leaderstats.Coins
local PlayerUserId = "player_"..v.UserId
local data = {}
local dataweapons = weapons:GetChildren()
data[weapons] = dataweapons
data[coins] = coins.Value
for i, v in pairs(data) do
print(v)
end
local success, err = pcall(function()
ds:SetAsync(PlayerUserId, data)
end)
if success then
print("data saved succesfully")
end
end
end)
The data isnt saving, maybe it just cant in time, or maybe im missing something important, well at the least I have found that its not that the data isnt able to go into the datastore, its that the data doesent save at all