Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

My zombies aren't being destroyed once reaching the end part?

In my tower defense game, there are waypoints in a folder, which zombies have to travel to.

Once they reach the last waypoint in the folder, they are normally supposed to be destroyed. However, it gives me the error "42 is not a valid member of Folder "Workspace.Grassy.waypoints".

local PhysicsService = game:GetService("PhysicsService")
local ServerStorage = game:GetService("ServerStorage")
local mob = {}

function mob.Move(mob, map)
	local humanoid = mob:WaitForChild("Humanoid")
	local waypoints = game.Workspace.Grassy.waypoints

	for waypoint=3, #waypoints:GetChildren() do
		humanoid:MoveTo(waypoints[waypoint].Position)
		humanoid.MoveToFinished:Wait()
	    
	end
	
	mob:Destroy()
end

function mob.Spawn(name, quantity,  map)
	local mobExists = ServerStorage.Mobs:FindFirstChild(name)
	
	if mobExists then
		for i=1, quantity do
			task.wait(0.5)
			local newMob = mobExists:Clone()
			newMob.HumanoidRootPart.CFrame = map.Start.CFrame
			newMob.Parent = workspace.Mobs
			newMob.HumanoidRootPart:SetNetworkOwner(nil)
			
			for i, object in ipairs(newMob:GetDescendants()) do
				if object:IsA("BasePart") then
					PhysicsService:SetPartCollisionGroup(object, "Mob")
				end
			end
			
			newMob.Humanoid.Died:Connect(function()
				task.wait(0.03)
				newMob:Destroy()
			end)
			
			coroutine.wrap(mob.Move)(newMob, map)
		end
	else
		warn("rip")
	end
end

return mob

When the amount of waypoints are less than 30..It works again..However when I place back all the waypoints leading up to 41 it starts malfunctioning again. Any help?

1 comment

0

"42 is not a valid member of Folder "Workspace.Grassy.waypoints" This error means that the 42nd waypoint doesn't exist in the waypoints folder. Adding a while loop might work

3 answers

In line 15, inside the parenthesis, why is there a "mob" inside when there's already a table named "mob", try changing it to function mob.Move(mobCharacter, map) maybe that's what causing the error.

4 comments

0

I don't understand what you mean, any other explanations?

0

Look at line 5, line 6, and line 15. You are creating a new variable named mob, when you already have a variable table also named "mob". You cannot creating two variables with the same name. In line 15, you tried to delete a table (which you cannot) and you meant to destroy the zombie.

0

And btw, table/variable table looks like "{}"

0

Hi! I've found the fix! It was because the amount of parts was higher than 41, meaning that it thinks there are 42 parts. The script was checking how many PARTS THERE WERE, which was the error. Thank you, anyways!

for waypoint=3, #waypoints:GetChildren()-2 do

OR

for waypoint=1, #waypoints:GetChildren() do

Fixed code

function mob.Move(mob, map)
    local humanoid = mob:WaitForChild("Humanoid")
    local waypoints = workspace.Grassy.waypoints
    local waypoint = 3

   while waypoint <= #waypoints:GetChildren() do
        humanoid:MoveTo(waypoints[waypoint].Position)
        humanoid.MoveToFinished:Wait()
        waypoint += 1
   end

    mob:Destroy()
end

I don't think I have to explain this one.

This discussion is preserved as it was. New answers, comments, and votes are closed.