#NPC not getting destroyed at the last check point
1 messages · Page 1 of 1 (latest)
Hello there! Great question and start. Can you perhaps tell me what you thought about when making the code? What are you trying to do?
I want to ask, does this line:
print("[NPCMove] " .. npcModel.Name .. " reached final checkpoint. Destroying NPC.")
ever get executed?
im trying to make a spin off of a steal a brainrot game and no it dosent get excuated
Oh, so when it reaches the black tunnel it should be destroyed, is that what?
Thats right
K. I think there's somewhere in the code where after checkpoint 15, it resets it back to 1.
okay nice thank you
Really dang mind if you can help me
You see here current > #checkpoints
Just make it >= or current > #checkpoints or current == #checkpoints
** You are now Level 6! **
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()
okay thanks ill let you know if it works thank you very much
If that doesn't work, I have a follow up question: how many checkpoints are there? 15?
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)
for some reason i have another promblem they are not going above check point 1
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)
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
so it does get deleted?
yes but at checking point 9 for some reason
Are you sure each NPC has their own current value? Again, they might be sharing the same variable.
i only have one
one NPC?
yes
can you add a print() statement after the current += 1 part and make it print the current value? then if possible, record that.
Sure
** You are now Level 3! **
sorry it a little hard to see
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.
okay ill try but do you still need that recprding