Datastore Tables

I’m trying to make a datastore table but I have no idea how to. I know that this post is short on context, but basically what I’m trying to make is a table that has all your “abilities” in it.

Are you talking something like this?

local TableToSave = { -- Save this table?
    Sprint;
    Camoflauge;
}

Yea, but how do I try using a for loop on it? When I try it, it doesn’t work
error: invalid argument #1 to ‘pairs’ (table expected, got nil) - S

Can you show your script? It’ll be helpful.

I have quite a long script so I’ll just send the part of the script that has the error.

local player_towers_datastore = datastoreservice:GetDataStore("player_towers")

local rep = game:GetService("ReplicatedStorage")
local event = rep:WaitForChild("GiveTower")

local cash
local data
local player_towers

game.Players.PlayerAdded:Connect(function(player)
	
	local cash = Instance.new("IntValue")
	cash.Parent = player
	cash.Name = "Cash"
	local player_user_ID = player.UserId

	
	local success, errormessage = pcall(function()
		data = player_cash_datastore:GetAsync(player_user_ID)
		player_towers = player_towers_datastore:GetAsync(player_user_ID)
	end)

	if success then
		cash.Value = data
		player.PlayerGui:WaitForChild("ScreenGui").PlayerMoney.Text = ("$"..data)
		for name, has_tower in pairs(player_towers) do
			if has_tower == true then
				event:FireClient(player, name)
			end
		end
	end

Sorry if it’s a bit messy. I think I know the problem, but I just don’t know how to solve it

1 Like

Can you show where you insert a value into the player_towers?

Edit:

I think it should be player_towers = {}

Wait, I’m sorry if this is obvious, I’m new to datastores.
So, do you have to show that player_towers is a table?

Probably since it’s a blank variable. A blank variable is counted as nil.

So the error was right. It got nil because it was a blank variable. It should be a table.

What if I want the table to have something in it? For example,

local player_towers = {
  hi = false,
  red = true
}

Can you explain what you want to add? And what’s the bool for?

I don’t really want to add anything, its just that I’m a bit confused right now.
So, lets say that I’m trying to make a table that has abilities in it, and if I want the values inside the table to change depending on the player’s data. Then what do I do?

Not gonna lie I’m also confused on what to do now. Try this post?

You could’ve do

for data, value in pairs(DataTable) do
    -- use your logic in here
end