How do i fix this?

“output in the Local Server” Do you mean the Output box in Studio?

Yes, in a Local Server multiple studio windows should open, check the output in all of them.

This is in the Server Window:
image

And Client Window:
image

Try this version of your script that I modified:

print("Check 1")
local Players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService")
local CashDS = DataStore:GetDataStore("CashDataStorage")
local Autosave = 10

Players.PlayerAdded:Connect(function(player)
	print("Check 2")
	local savedTable
	local s,m = pcall(function()
		savedTable = CashDS:GetAsync(player.userId)
	end)
	if typeof(savedTable) ~= "table" then
		savedTable = {0,0,0}
	end

	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "SavesFolder"
	leaderstats.Parent = player

	local Cash2 = Instance.new("StringValue")
	Cash2.Name = "AccDisabled"
	cash2.Parent = leaderstats

	local Cash = Instance.new("StringValue")
	Cash.Name = "Save1"
	cash.Parent = leaderstats
	
	local Cash3 = Instance.new("StringValue")
	Cash3.Name = "PlayerPasswort"
	cash3.Parent = leaderstats
	
	Cash2.Value = tonumber(savedTable[1]) or 0
	Cash.Value = tonumber(savedTable[2]) or 0
	Cash3.Value = tonumber(savedTable[3]) or 0
	print("Check 3")
	while true do
		wait(Autosave)
		if not Players:FindFirstChild(tostring(player)) then break end
		print("Saving data for: ", player)
		local saved = false
		local s,m = pcall(function()
			CashDS:SetAsync(player.UserId, {Cash2.Value, Cash.Value, Cash3.Value})
			saved = true
		end)
		print("Players data was saved: ", saved)
	end
end)

and see if all 3 prints will print.

1 Like

It looks like your GetAsync() call returns nil. What’s on line 33?

image

So, since newcash is nil since the player has no data, it is telling you that you are trying to index a nil value. So, you need to put an if statement there and check if newcash exists. If it does, then you index it and set the Cash2, Cash, and Cash3 values, otherwise you would set those values to 0.

Are you guys going to ignore the script I posted above which fixes the error you guys are currently talking about? lol

im testing it right now. ---------

Umm… I’d rather run someone through debugging their problems themselves so that they can do it better next time, rather than provide them with the script that fixes all their problems magically.

1 Like

Yes that is a good thing to do but my point was first seeing if it would print all 3 prints before the autosave loop. Then I was going to explain what it was that I did to fix it.

You don’t need to use a bunch of if statements to check if something exists, if its a number. You can use tonumber() and it will catch it for you and then you can use “or 0” to assign it so its not trying to put false/nil into the stringvalue.

It prints all Checks in Studio and works in Studio but still doesnt save in Game

He’s trying to save the values to a table and then access those values. In order to do that, you need to check if the data is saved, because if it returns a nil value you cannot use a ternary operator on it since it would error if you tried indexing the nil value, because you are expecting it to be a table. And you can’t do it the other way around either because in that cause it would always choose 0.

I have checked for this under the first pcall inside PlayerAdded in the script I provided.

I am not sure why its not working outside of studio, if its the same script being used.

My Bad the script works fine i just forgot to enabled another script

Thanks Guys for helping me, you really helped me!

1 Like

If you want to play my game im working on Click here