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

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

Asked by 2 years ago

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?

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 MarkedTomato 810 — 2y

3 answers

Log in to vote
0
Answered by 2 years ago

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.

0
I don't understand what you mean, any other explanations? EmmaMallen 13 — 2y
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. T3_MasterGamer 2189 — 2y
0
And btw, table/variable table looks like "{}" T3_MasterGamer 2189 — 2y
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! EmmaMallen 13 — 2y
Ad
Log in to vote
0
Answered by 2 years ago
Edited 2 years ago
for waypoint=3, #waypoints:GetChildren()-2 do

OR

for waypoint=1, #waypoints:GetChildren() do
Log in to vote
0
Answered by 2 years ago
Edited 2 years ago

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.

Answer this question