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

SetAsync / DataStoreService not working with table?

Gives me an error with "ServerScriptService.DataStore:33: attempt to index number with 'cash'


local DataStoreService = game:GetService("DataStoreService")
local cashStore = DataStoreService:GetDataStore("PlayerCash")


game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = leaderstats
	
	local Wins = Instance.new("IntValue")
	Cash.Name = "Wins"
	Cash.Parent = leaderstats
	
	local playerUserId = player.UserId
	
	local data
	
	local succ, err = pcall(function()
		data = cashStore:GetAsync("Player_"..playerUserId)

		
	end)
	
	if succ then
			
			Cash.Value = data.cash
			Wins.Value = data.wins	
		
	elseif err then
		
		print(err)
		
	end
	
end)


game.Players.PlayerRemoving:Connect(function(player)
	
	local playerUserId = player.UserId 
	
	local storedIntegers = {
		
		cash = player.leaderstats.Cash.Value;
		wins = player.leaderstats.Wins.Value;
		
	}
	
	local succ, succ = pcall(function()
		
		cashStore:SetAsync("Player_"..playerUserId,storedIntegers)
		
	end)
	
end)

2 comments

0

I think you have to put Cash.Value = data.Cash Wins.Value = data.Wins instead of Cash.Value = data.cash Wins.Value = data.wins but i'm not sure

0

False, i have two variables inside the table that are called "cash", "wins", which i'm trying to access from when the pcall is called success.

1 answer

[SOLVED]

Seems to be that everything worked fine, but for some reason, I had to add an if statement to if the data exists.

REVAMPED CODE :


local DataStoreService = game:GetService("DataStoreService")
local cashStore = DataStoreService:GetDataStore("PlayerIntegerSave")


game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local Cash = Instance.new("IntValue")
	Cash.Name = "Cash"
	Cash.Parent = leaderstats
	
	local Wins = Instance.new("IntValue")
	Wins.Name = "Wins"
	Wins.Parent = leaderstats
	
	local playerUserId = player.UserId
	
	local data
	
	local succ, err = pcall(function()
		data = cashStore:GetAsync("Player_"..playerUserId)

		
	end)
	
	if succ then
		
		if data then
			

			Cash.Value = data.cash
			Wins.Value = data.wins	


			
		end
		
	elseif err then
		
		print(err)
		
	end
	
end)


game.Players.PlayerRemoving:Connect(function(player)
	
	local playerUserId = player.UserId 
	
	local data = {
		
		cash = player.leaderstats.Cash.Value;
		wins = player.leaderstats.Wins.Value;
		
	}
	
	local succ, err = pcall(function()
		
		cashStore:SetAsync("Player_"..playerUserId,data)
		
	end)
	
end)

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