Number is not a valid member of folder

Getting this weird error when I use math.random, and there doesn’t seem to be many solutions on the DevForum. This might be an oversight on my part, if you have any solutions or work arounds please let me know.


local Lobby = workspace:WaitForChild("Lobby")
local KeyAssets = Lobby:WaitForChild("KeyAssets")
local NPC_Locations = KeyAssets:WaitForChild("NPC_Locations")

local function RandomSeat(NPC)
	local GetNPCLocations = NPC_Locations:GetChildren()
	local RandomWoodenStool = NPC_Locations[math.random(1, #GetNPCLocations)] --// error happens here
	local Occupied = RandomWoodenStool:GetAttribute("Occupied")
	if Occupied == true then
		RandomSeat()
	else
		Occupied = true
		local Seat = RandomWoodenStool:FindFirstChildOfClass("Seat")
		CalculatePath(NPC, Seat.Position)
	end
end

image

The error you’re encountering is likely due to the fact that NPC_Locations is a Folder object, and you’re trying to access its children using indexing instead of the GetChildren() method. To fix this issue, modify the line where the error occurs as follows:

local RandomWoodenStool = GetNPCLocations[math.random(1, #GetNPCLocations)]

By using GetNPCLocations instead of NPC_Locations , you will correctly access the children of the NPC_Locations folder and eliminate the error.

1 Like

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