How do i fix this?

I have a DataStore that worked fine untill now. Now it just says in the Console:
“Workspace.Data Save Stuff.Data Saver:26: attempt to index number with number”
and i dont know why.

Here is my code

local DataStore = game:GetService("DataStoreService")
local CashDS = DataStore:GetDataStore("PlayerData")
local Autosave = 10

game.Players.PlayerAdded:Connect(function(player)
	
	local Cashsuccess, newcash = pcall(function() 
	  return CashDS:GetAsync(player.userId) or 0
	 end)
	
	
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "SavesFolder"
	leaderstats.Archivable = true
	
	local Cash2 = Instance.new("StringValue",leaderstats)
	Cash2.Name = "AccDisabled" 
	
	local Cash = Instance.new("StringValue",leaderstats)
	Cash.Name = "Save1" 
	
	local Cash3 = Instance.new("StringValue",leaderstats)
	Cash3.Name = "PlayerPasswort" 
	
	
	Cash2.Value = newcash[1]; -- Error is Here
	Cash.Value = newcash[2];
	Cash3.Value = newcash[3];
	
	while wait(Autosave) do
		
		print(""..player.Name.."'s Data is Saving...")
	 
	local Cashsuccess, ValueAmmount = pcall(function()
			return CashDS:SetAsync({player.userId,Cash2.Value, Cash.Value, Cash3.Value})
	 end)

	 if Cashsuccess then
		end
	end
end)


1 Like
 return CashDS:GetAsync(player.userId) or 0

If a new player joins the game then CashDS:GetAsync(player.userId) will always return false meaning that 0 is stored. What you should have instead is a table of default values ie:

local newCash;

local Cashsuccess, errormsg = pcall(function() 
	newCash = CashDS:GetAsync(player.userId) or {
	    "DefaultValue1";
	    "DefaultValue2";
	    "DefaultValue3";
	};
end);

i have the same error when i put this in my script

I put another edit onto the previous post that I forgot to change.

am i doing something wrong or i dont get it. Is this the right way

	while wait(Autosave) do

		print(""..player.Name.."'s Data is Saving...")
		local newCash;

		local Cashsuccess, errormsg = pcall(function() 
			newCash = CashDS:GetAsync(player.userId) or {
				"0";
				"0";
				"0";
			};
		end);

if this is correct then its not saving anythig

What are the 0’s for, if i knew i could better understand the problem

Default values for the script to use

but what is the part trying to do, is it setting data to 0 if there isnt any data?

yes, this is to prevent the script from getting values that don’t exist

maybe this will work then

data = CashDS:GetAsync(player.userId)
if data then
-- Give the player the things from their data, such as money
else
-- Make the players data default data like 0 money because you start with that
end

Edit the example to do what you want it to do

now it doesn’t give me errors but it doesn’t save any data

maybe try this then

	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "SavesFolder"
	leaderstats.Archivable = true
	
	local Cash2 = Instance.new("StringValue",leaderstats)
	Cash2.Name = "AccDisabled" 
	
	local Cash = Instance.new("StringValue",leaderstats)
	Cash.Name = "Save1" 
	
	local Cash3 = Instance.new("StringValue",leaderstats)
	Cash3.Name = "PlayerPasswort"

       data = CashDS:GetAsync(player.userId) 
       if data then
-- Give the player the things from their data, such as money
      else
	Cash2.Value = 0
	Cash.Value = 0
	Cash3.Value = 0
end
1 Like

i have tryed this but it didnt work i even tryed other DataStore scripst from the Toolbar but nothing is saving any data and i have all the things that Datastores need enabled.

does studio have access api services

yes api services are enabeld. I tryed it in other puplic games of mine and nothing is working

delete the parts on cash cash2 and cash3 where their names are changed and see if that helps anything

i have used my old script for that and it works but i get error when i try to load the data.

Maybe this will work?

local Cashsuccess = pcall(function()
        CashDS:SetAsync(player.userId, Cash2.Value)
        CashDS:SetAsync(player.userId, Cash.Value)
        CashDS:SetAsync(player.userId, Cash3.Value)
 end)

it works but when i try to set the values to the saved value i again get this error
“attempt to index number with number” whitch was my problem in the first place.

I forgot, it kinda works couse it only saved one Value

I’m kinda lost where you are at this point. The reason it wasn’t saving the data is because you used :GetAsync() instead of :SetAsync() to try and save the day. Now, here you need to use :SetAsync() once and save the whole table of values, not overwrite them with the different cash’s values. Try saving it like this:

local Cashsuccess = pcall(function()
           CashDS:SetAsync(player.UserId, {Cash2.Value, Cash.Value, Cash3.Value})
end)