Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Check if returned variable is a table?

Asked by 4 years ago

Hi there!

I'm working on a pathfinding script for an enemy in my game and i'd run into a bit of a problem.

I use a module script to access some important functions such as finding the closest target, a random target, and the function that we need right now: the one that creates the path towards the target.

here's what it looks like:

path:ComputeAsync(start, goal)
if path.Status == Enum.PathStatus.Success then
    local waypoints = path:GetWaypoints()
    return waypoints
else
    return nil
end

so as you can see, if it creates a path, that's what it returns! all the waypoints. otherwise it returns nothing. the problem is, i've run into a situation where it returns something (in that specific scenario it returned a boolean) and because it wasn't a table, the NPC's script broke!

here's why (the NPC's script):

if returnedWaypoints ~= nil and #returnedWaypoints > 0 then

if it returns something other than a table it'll break because i can't do #returnedWaypoints on anything other than a table!

so my question is; how do i check if returned value is a table? i've tried to look up a few ways using type() and typeof() but i couldn't find anything useful ;w;

thanks in advance! any ideas are appreciated <3

1 answer

Log in to vote
1
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

ROBLOX has a new global function called typeof(). It is practically identical to the type fiction, however it contains a more vast and versatile return library: E.g.

local userdata = Vector3.new()
print(type(userdata)) --< 'Userdata'
print(typeof(userdata)) --< 'Vector3'
--// 'typeof()' is not apart of the default Lua language

Let's take a gander at what this is will look like in application:

local datatypeExample = {"Hello", "world!"};

if (typeof(datatypeExample) == 'table') then
    print("Table is a "..typeof(datatypeExample))
else
    print(typeof(datatypeExample)
end

For a debounce you can use a return end.

if (typeof(returnTable) ~= 'table') then return end
0
thank you! i can't test it out right now but when i do and make sure it works I'll accept your answer ASAP! iiSpeak_Lua 35 — 4y
Ad

Answer this question