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

How can I make an if statement for a table?

Hey guys I have followed this wiki http://wiki.roblox.com/index.php?title=Saving_Player_Data and have made a script with tables like so

-- Setup table that we will return to scripts that require the ModuleScript.
local PlayerStatManager = {}
 
local DataStoreService = game:GetService('DataStoreService')
local playerData = DataStoreService:GetDataStore('PlayerData')

-- Table to hold all of the player information for the current session.
local sessionData = {}
 
-- Function the other scripts in our game can call to change a player's stats. This
-- function is stored in the returned table so external scripts can use it.
function PlayerStatManager:ChangeStat(player, statName, changeValue)
	sessionData[player][statName] = sessionData[player][statName] + changeValue
end
 
-- Function to add player to the sessionData table.
local function setupPlayerData(player)
	local data = playerData:GetAsync(player.UserId)
	if not data then
		data = {hp = 0, xp = 0}
		playerData:SetAsync(player.UserId, sessionData[player])
	end
	sessionData[player] = data
end
 
-- Bind setupPlayerData to PlayerAdded to call it when player joins.
game.Players.PlayerAdded:connect(setupPlayerData)

local function savePlayerData(player)	
playerData:SetAsync(player.UserId, sessionData[player])
end
 
game.Players.PlayerRemoving:connect(function(player)
	savePlayerData(player)
	-- We don't need to keep track of the data anymore
	sessionData[player] = nil
	local DataStore = game:GetService("DataStoreService")
	local ds1 = DataStore:GetDataStore("coincurrency")
	print("Saving currency to data store...")	
	ds1:SetAsync(player.UserId, player.leaderstats.Coins.Value)	
	print("Currency Saved")
end)

-- Return the PlayerStatManager table to external scripts can access it.
return PlayerStatManager

I want to have another script that will check if the values hp and xp are a certain number and then do something if they are, so I tried making this script

local DataStoreService = game:GetService('DataStoreService')
local playerData = DataStoreService:GetDataStore('PlayerData')

local function setupPlayerData(player)
local data = playerData:GetAsync(player.UserId)
if data = {hp = 0, s = 0} then
print("Blah")
end
end

Though as you can see....this does not seem to be correct, I hope this is not a request but can someone steer me into the right direction as to what I need to do? I am confused as to how this works in general.

1 comment

0

Edited to fix code formatting

1 answer

Comparing tables using == compares their identity. That is, it asks whether they're the same table[^1], and not where they hold the same stuff.

You have to explicitly compare your tables. For example, if you want the .hp and .s properties to match, you can just check both of those:

if data.hp == 0 and data.s == 0 then

If you find yourself doing this a lot, you might want to write a helper method to do the comparison.

Though, in practice, I don't think you'll often need to compare whole tables to each other.

[^1]: e.g., a = {}; b = {}; A = a;. They all hold the same stuff (nothing). But if I change a by saying a.x = 5, then A.x is also 5 (since A and a are the same object) but b.x is still nil. Thus a == A but a ~= b.

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