Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why Do I Get The Error "cash is not a valid member of Folder Players.dochooideveloper.leaderstats"?

Hello This Is My Second Post Here I Would Like To Ask Why This Error Comes Up When I Try To Add Or Save The value "cash" Here Is My Code :

local DataStoreService = game:GetService("DataStoreService")

local DataStore = DataStoreService:GetDataStore("DataStore")

game.Players.PlayerAdded:Connect(function(player)
	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	

	local cash = Instance.new("IntValue")
	cash.Name = "Cash"
	cash.Value = 100
	cash.Parent = stats
	stats.Parent = player
	
	
	
	local data
	local success,errorMessage = pcall(function()
		data = DataStore:GetAsync(player.UserId.."-cash")
	end)
	
	if success then
	 cash.Value = data
	else
		warn("There Was An Error Whilst Fetching Data. Cause: "..errorMessage.."")
	end
	
end)

game.Players.PlayerRemoving:Connect(function(player)
	local success,errorMessage = pcall(function()
		DataStore:SetAsync(player.UserId.."-cash",player.leaderstats.cash.Value)
	end)
	
	if success then
		print("Data Successfully Saved")
	else
		print("There Was An Error When Saving Data!")
		warn(errorMessage)
	end
end)

4 comments

0

Parent the stats folder to the player, add "stats.Parent = players" on line 8. Add [Solved] to the title if this solved your problem.

0

^Yes

0

how do i change the title

0

There should be an edit button somewhere next to it.

1 answer

✓ Accepted answer

Do player.leaderstats.Cash.Value, because you set the name for cash as "Cash" with an uppercase C.

1 comment

0

cash is the variable not the object.

This discussion is preserved as it was. New answers, comments, and votes are closed.