#NPC not getting destroyed at the last check point

1 messages · Page 1 of 1 (latest)

hard otter
#

Hello im beginner to robloc scripting and i quite cant figure out why my npc arent getting destoryed adn this is a script not a local script in sever service

waxen terrace
#

I want to ask, does this line:
print("[NPCMove] " .. npcModel.Name .. " reached final checkpoint. Destroying NPC.")
ever get executed?

hard otter
waxen terrace
hard otter
#

Thats right

waxen terrace
#

K. I think there's somewhere in the code where after checkpoint 15, it resets it back to 1.

hard otter
#

okay nice thank you

waxen terrace
#

ohhh

#

There's a logic issue I think.

hard otter
#

Really dang mind if you can help me

waxen terrace
#

You see here current > #checkpoints
Just make it >= or current > #checkpoints or current == #checkpoints

opal nebulaBOT
#

studio** You are now Level 6! **studio

waxen terrace
#

delete that part here:
humanoid.MoveToFinished:Once(function()
print("[NPCMove] " .. npcModel.Name .. " reached " .. target.Name)
if current == #checkpoints then
print("[NPCMove] " .. npcModel.Name .. " completed all checkpoints. Destroying NPC.")
npcModel:Destroy()
return
end
current += 1
moveToNext()

hard otter
#

okay thanks ill let you know if it works thank you very much

waxen terrace
#

If that doesn't work, I have a follow up question: how many checkpoints are there? 15?

hard otter
#

yes

#

their is 15

waxen terrace
#

Do you mind making there only one NPC, and print out the "current" value in NPC Move loop? Watch the number to see if it goes above 15.

#

(If the fix I told you doesn't work)

hard otter
#

for some reason i have another promblem they are not going above check point 1

waxen terrace
#

I think I understand the issue. The variable current is being used by multiple NPCs, so they each keep resetting and changing it for one another.

#

Make sure each NPC has their own current counter. For example:

local function startNPCMovement(npcModel, checkpoints)
    local current = 1
    local humanoid = npcModel:FindFirstChildWhichIsA("Humanoid")

    local function moveToNext()
        if current > #checkpoints then
            print("[NPCMove] " .. npcModel.Name .. " reached final checkpoint. Destroying NPC.")
            npcModel:Destroy()
            return
        end

        local target = checkpoints[current]
        if target and target:IsA("BasePart") then
            print("[NPCMove] " .. npcModel.Name .. " moving to " .. target.Name)
            humanoid:MoveTo(target.Position)

            humanoid.MoveToFinished:Once(function()
                print("[NPCMove] " .. npcModel.Name .. " reached " .. target.Name)
                current += 1
                moveToNext()
            end)
        else
            warn("[NPCMove] Checkpoint " .. tostring(current) .. " is missing or not a BasePart.")
        end
    end

    moveToNext()
end

then loop through each NPC and do:

startNPCMovement(npcModel, checkpoints)
hard otter
#

Okay ill try then ill see what happends

#

do you want me a record whats happening like the full thing

#

this is what i notice when they checkpoint 15 they go back to chekpoint 4 and the checkpoint 9 they get deleted

waxen terrace
#

so it does get deleted?

hard otter
#

yes but at checking point 9 for some reason

waxen terrace
#

Are you sure each NPC has their own current value? Again, they might be sharing the same variable.

hard otter
#

i only have one

waxen terrace
#

one NPC?

hard otter
#

yes

waxen terrace
#

can you add a print() statement after the current += 1 part and make it print the current value? then if possible, record that.

hard otter
#

Sure

opal nebulaBOT
#

studio** You are now Level 3! **studio

hard otter
waxen terrace
#

I don't see anything that hints at it getting destroyed at block 9.

#

Again, you need to make sure each NPC has its own current counter.

#

If possible, test it with only one NPC. No more spawning. Just one.

#

If with only ONE NPC walking around and it does get destroyed when it reaches the tunnel, then yes, the problem is with each NPC not having their own current variable.

#

@hard otter I just thought about it. If you need a quick and easy fix, add an invisible part in front of the tunnel. Add code in the NPCs' code so that: if they touch the part, they get destroyed.

This bypasses the logic issues, but isn't long term and you probably want to know what to do in case this happens again.

hard otter
#

okay ill try but do you still need that recprding