so i am working on a game and i noticed that data doesn’t save at all, although the script is in serverscriptstorage and it’s a server script
i also tried to format the code in a more readable way but it still didn’t do anything
here’s the leaderstats and datasave code:
local DSS = game:GetService("DataStoreService")
local coinslot = DSS:GetDataStore("coinslot")
game.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.Value = 0
coins.Parent = leaderstats
while wait(3) do
coins.Value += 1
end
local plrid = 'Player_'..player.UserId
local data
local success, errorMessage = pcall(function()
data = coinslot:GetAsync(plrid)
end)
if success then
coins.Value = data
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local plrid = 'Player_'..player.UserId
local coins = player.leaderstats.Coins.Value
local success, errorMessage = pcall(function()
coinslot:SetAsync(plrid, coins)
end)
if not success then
warn(errorMessage)
end
end)
Are you playtesting in Studio - if so, is the “Enable Studio Access to API Services” option on? This is accessible from game settings. If it’s off, Studio won’t be allowed to access your actual datastore, and will instead pretend to be using it.
It stops at this line and doesn’t do any lines after that.
These loops yield the script.
You should make a separate script which doesn’t make a new loop for every joined player so these loops don’t pile up and make the game lag like crazy
I would just say put it inside a spawn(function() end) but you would probably be coming back trying to figure out why the game lags like crazy as more players join.
Remove those lines and make a new script with this
while task.wait(3) do
for i,v in ipairs(game.Players:GetPlayers()) do
if v:FindFirstChild("leaderstats") then
if v.leaderstats:FindFirstChild("Coins") then
v.leaderstats.Coins.Value += 1
end
end
end
end