Datasave not working although no warnings or errors in the output

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)

can anyone help me out? thanks

1 Like

when you use a pcall it isnt going to log any error or warnings you have to do that yourself

after the pcall print the error message and see what it saids

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.

thanks for the question but i have it on

i tried your method but it won’t display any error message

does it print nil or just absolutely nothing

it will print out nil instead of an error

print data and see what it returns

it won’t print anything at all

are you sure the function is running

Give Datastore2 a try, it will fix all your problems.

1 Like

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
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.