My script doesn’t work but there’s no error in the output.
local DataStoreService = game:GetService("DataStoreService")
local MyDataStore = DataStoreService:GetDataStore("ILOVESUSHI123")
game.Players.PlayerAdded:Connect(function(player)
local PlayerStats = Instance.new("Folder", player)
PlayerStats.Name = "leaderstats"
local Coins = Instance.new("IntValue", PlayerStats)
Coins.Name = "Coins"
local Cash = Instance.new("IntValue", PlayerStats)
Cash.Name = "Cash"
local Stars = Instance.new("IntValue", PlayerStats)
Stars.Name = "Stars"
local success, errorMessage = pcall(function()
local PlayerStatsData = MyDataStore:GetAsync(player.UserId.."-PlayerData")
for i, statsaved in pairs(PlayerStatsData) do
for i, stat in pairs(player:WaitForChild("PlayerData"):GetChildren()) do
if statsaved == stat.Name then
stat.Value = statsaved
end
end
end
end)
if success then
print("Data saved!")
else
print("Data error!")
warn("Error!")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, errorMessage = pcall(function()
local PlayerStats = player:WaitForChild("PlayerStats")
local SaveData = {Coins = 0, Cash = 0, Stars = 0}
for _, v in pairs(PlayerStats:GetChildren()) do
if v.Name == "Coins" then
SaveData.Coins = v.Value
elseif v.Name == "Cash" then
SaveData.Cash = v.Value
elseif v.Name == "Stars" then
SaveData.Stars = v.Value
else
print("Error, nil or idk")
end
end
MyDataStore:SetAsync(player.UserId.."-PlayerData", SaveData)
end)
if success then
print("Data saved!")
else
print("Data error!")
warn("Error!")
end
end)
Ive also had the same error and i never found it. Unlucky you but my best solution is to write it again WORD for WORD (except for the leaderstats, just keep those)
and PlayerStatsData may be nil, then pairs doesn’t work, since it needs a table.
Code
local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local MyDataStore = DataStoreService:GetDataStore("ILOVESUSHI123")
local function Remove(player)
-- Get to save --
local PlayerStats = player:WaitForChild("PlayerStats")
local SaveData = {
Coins = PlayerStats.Coins.Value,
Cash = PlayerStats.Cash.Value,
Stars = PlayerStats.Stars.Value
}
-- Save it --
local success, Error = pcall(MyDataStore.SetAsync, MyDataStore, player.UserId.."-PlayerData", SaveData)
if success then
print("Data saved!")
else
print("Data error!")
warn("Error:", Error)
end
end
Players.PlayerAdded:Connect(function(player)
local PlayerStats = Instance.new("Folder", player)
PlayerStats.Name = "leaderstats"
local Coins = Instance.new("IntValue", PlayerStats)
Coins.Name = "Coins"
local Cash = Instance.new("IntValue", PlayerStats)
Cash.Name = "Cash"
local Stars = Instance.new("IntValue", PlayerStats)
Stars.Name = "Stars"
-- Get --
local success, Data = pcall(MyDataStore.GetAsync, MyDataStore, player.UserId.."-PlayerData")
if not success then
print("Data error!")
return warn("Error:", Data)
end
Data = Data or {}
-- Load --
for i, stat in pairs(player:WaitForChild("PlayerData"):GetChildren()) do
stat.Value = Data[stat.Name] or 0
end
end)
Players.PlayerRemoving:Connect(Remove)
game:BindToClose(function()
for _, v in pairs(Players:GetPlayers()) do task.spawn(Remove, v) end
task.wait(3)
end)
If this solves your problem, mark it as a solution.