Hey, I am new to this site and a bit new to lua so excuse any mistakes please.

I've tried numerous things to get this code to work and I can't seem to get it to work.

Basically I have a datastore and I want my players data to save automatically, so I made a basic autosave loop, except when the loop executes, I get the error "Attempt to index global 'CashDS' (a nil value)"

Can someone help me and tell me whats wrong? Im assuming that the value is already nil because of the error but I don't know why considering im setting it to 10 if its nil when the player joins.

Edit: I decided to add the very full error message here: 20:44:20.211 - ServerScriptService.Data:61: attempt to index global 'CashDS' (a nil value)

My data code is:

DataStore = game:GetService("DataStoreService")
game.Players.PlayerAdded:Connect(function(plr)
    Prefix = "Data_Id_"..plr.UserId
	local GameData = DataStore:GetDataStore("Data_User_Id_"..plr.UserId)
    CashDS = GameData:GetAsync("Cash")
    GoldDS= GameData:GetAsync("GoldDS")
    LevelDS = GameData:GetAsync("Level")
    EXPDS = GameData:GetAsync("EXP")
    EXPNeededDS = GameData:GetAsync("EXPNeeded")
    ExploitPointsDS = GameData:GetAsync("Synapse")
    BackpackSpaceDS = GameData:GetAsync("Space")
    MaxSpaceDS = GameData:GetAsync("MaxSpace")
    BountyAmountDS = GameData:GetAsync("Bounty")
    
	local Data = Instance.new("Folder",plr)
    Data.Name = "Data"

	Cash = Instance.new('IntValue',Data)
	Cash.Name = "Cash"
	Cash.Value = CashDS or 10

	Gold = Instance.new('IntValue',Data)
	Gold .Name = "Gold"
	Gold .Value = GoldDS or 0

	Level = Instance.new('IntValue',Data)
	Level.Name = "Level"
	Level.Value = LevelDS or 0

	EXP = Instance.new('IntValue',Data)
	EXP.Name = "EXP"
	EXP.Value = EXPDS or 0

	EXPNeeded = Instance.new('IntValue',Data)
	EXPNeeded.Name = "EXPNeeded"
	EXPNeeded.Value = EXPNeededDS or 150

	ExploitPoints = Instance.new('IntValue',Data)
	ExploitPoints.Name = "Synapse"
	ExploitPoints.Value = ExploitPointsDS or 0

	BackpackSpace = Instance.new('IntValue',Data)
	BackpackSpace.Name = "Space"
	BackpackSpace.Value = BackpackSpaceDS or 0

	MaxSpace = Instance.new('IntValue',Data)
	MaxSpace.Name = "MaxSpace"
	MaxSpace.Value = MaxSpaceDS or 10
	
	BountyAmount = Instance.new('IntValue',Data)
	BountyAmount.Name = "Bounty"
	BountyAmount.Value = BountyAmountDS or 0

My whole autosave code is:

while wait(10) do
		print("Attempting to autosave player data...")
		CashDS:SetAsync(Prefix, Cash.Value) --Error happens here
    	GoldDS(Prefix, Gold.Value)
    	LevelDS:SetAsync(Prefix, Level.Value)
   	 	EXPDS:SetAsync(Prefix, EXP.Value)
   		EXPNeededDS:SetAsync(Prefix, EXPNeeded.Value)
   		ExploitPointsDS:SetAsync(Prefix, ExploitPoints.Value)
   		BackpackSpaceDS:SetAsync(Prefix, BackpackSpace.Value)
   		MaxSpaceDS:SetAsync(Prefix, MaxSpace.Value)
   		BountyAmountDS:SetAsync(Prefix, BountyAmount.Value)
		print("Data saved!")
	end

I have no idea what is wrong with my code. Can someone just point out whats wrong with my code?

7 comments

0

is the auto save code in another script?

0

Its in the same script as the data script, its still in the player join function too.

0

What line is the error.... Also why make it autosave and not saving on player removed?

0

Ah I see, line 3 of second code... Your method of saving data is highly inefficient, use a table to store data.

0

I used to use tables but my friend wants to use multiple datastores, and also it does save on player remove but its the same error on remove as well.

0

Totally unrelated tip. You should always save/load data from datastore inxprotected enviroment (pcall) as its off studio service and it can fail because many reasons.

0

Yeah I know karlo its just that I haven't finished the script yet, im just trying to make a basic save and load before I continue, so its one problem at a time instead of a thousand.