6 is not a valid member of Folder "Workspace.Ghosts"; weird error

Hello.

So what I am trying to do is make a randomly generated ghost NPC teleport to a random point inside of a separate folder.

The problem is that each time the NPC is teleported, it returns the number 6, and I do not know why.

Error:

Here is the code; (This code is for testing purposes, so for now, this would go inside of a ServerScript, inside of workspace).

local points = workspace:WaitForChild("Points")
local ghosts = workspace:WaitForChild("Ghosts")

local tweenservice = game:GetService("TweenService")

local test_t = 0.2
local active = true

task.spawn(function()
	while active do
		local randompoint = points[math.random(1, #points:GetChildren())]
		local randomghost = ghosts[math.random(1, #ghosts:GetChildren())]

		if randomghost then
			randomghost:WaitForChild("HumanoidRootPart").CFrame = randompoint.CFrame
		end

		task.wait(test_t)
	end
end)

Can anyone help me get a solution to this? Thank you.

you need to get the children of points and ghosts

local points = workspace:WaitForChild("Points")
local ghosts = workspace:WaitForChild("Ghosts")

local tweenservice = game:GetService("TweenService")

local test_t = 0.2
local active = true

task.spawn(function()
	while active do
		local randompoint = points:GetChildren()[math.random(1, #points:GetChildren())]
		local randomghost = ghosts:GetChildren()[math.random(1, #ghosts:GetChildren())]

		if randomghost then
			randomghost:WaitForChild("HumanoidRootPart").CFrame = randompoint.CFrame
		end

		task.wait(test_t)
	end
end)

Oh okay.

Thank you for helping me, this solution worked.

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