How can I structure an if statement that accounts for all three?

Code for the server side:

RequestAllOrSpecificAdditionalInfoRemoteFunction.OnServerInvoke = function(player, saveID)	
	
	local success, errorMessage = pcall(function() 
		AllAdditionalInfoData = dataStore:GetAsync(player.UserId .. "_AdditionalInfo") 
	end)

	if success and AllAdditionalInfoData then
		
		return AllAdditionalInfoData

	else

		warn("The Player has no Data!") 
		return false

	end
	
end

Code for the client side:
image

It either returns data, {} (the type is a table), or nil.

How can I structure an if statement (on the client side) that accounts for all three? Thanks!

1 Like

What do you mean by “all three”?

1 Like

All three of the possible things that the remote function returns.

1 Like

Well, when you’re using an if statement doing: if variable then will respond the same if the variable is false or nil. So that covers all 3 bases. If the data is nil it’ll perform the else function, if the data is false it’ll perform the else function and if the data is a table it’ll perform the function in the if statement

1 Like

So in this case, just do

if AllAdditionalInfoData then

else

end
1 Like

image

image

image

image

How do I separate from {} and {...}?

2 Likes

he wants to check if its an empty dictionary too:

if next(AllAdditionalInfoData) == nil then or if you know one of the indexes you can do if AllAdditionalInfoData.value then

3 Likes

Oh I see you wanted {} and {…} in that case:

if AllAdditionalInfoData and next(AllAdditionalInfoData) then

else

end
2 Likes

wont work if its a dictionary:
image

1 Like

Good point, he could just do if ~= {} then, updated my reply with this in mind

1 Like

actually this won’t work bc {} creates a different table in memory than the original empty one

2 Likes

Yeah, rip; next(table) == nil seems to be the most optimal way to do it.

Updated the script I gave, thanks for your help bg9

2 Likes

yep, next() is the way to do it :D

I tried:

for _, _ in pairs() do
    -- code here
    break
end

but it didn’t quite sit right with me, so I decided to make a forum post. Thank you all so much!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.