Table.find not working in if statement?

table.find is returning nil even though the table has the exact string it’s looking for.

for _,v in pairs(game:GetService("Players"):GetChildren()) do
	local x = table.find(PlayersPlaying:GetChildren(), v.Name)
	print(PlayersPlaying:GetChildren(), v.Name, x)
	if x then
		print("time")
		repeat task.wait() until v.Character
		v.Character:MoveTo(workspace.TemporarySpawn.Position)
	end
end

returns:


(playing with 2 people)
If anyone knows why this could happen, I would appreciate it!

The type of the array elements and the match must be the same. GetChildren returns { Instance }. You pass a string.

Instance:FindFirstChild is what you’re looking for.

Oh, I understand. thank you. so would the easiest way to do this be making a table with all the names in that folder and checking the new table?

You could create a dictionary where each key is the name of the corresponding instance. Normally, there would be the danger of conflicting names, but since this is for players, their names will never be the same.

local function map(instances: { Instance }): { [string]: Instance }
    local mapped = {}

    for _, instance in ipairs(instances) do
        mapped[instance.Name] = instance
    end

    return mapped
end

local playersPlaying = map(PlayersPlaying:GetChildren())

playersPlaying["PhoenixRessusection"]
playesrPlaying[v.Name]

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