#Should I use RunService or a true loop for this?

1 messages · Page 1 of 1 (latest)

halcyon quartz
#
EntityHandler.EntityStates = {
    Patrol = function(Npc,patrolFolder,path)
        while true do
            for _,goal in pairs(patrolFolder:GetChildren()) do
                local success,errormsg = pcall(function()
                    return path:ComputeAsync(Npc.HumanoidRootPart.Position,goal.Position)
                end)
                if success then
                    for _,waypoint in pairs(path:GetWaypoints()) do
                        Npc.Humanoid:MoveTo(waypoint.Position)
                        Npc.Humanoid.MoveToFinished:Wait()
                    end
                else
                    warn(errormsg)
                end
            end
        end
end,
}
#

if I did heartbeat it would run more than it would need to and also I tried didn't work,. while true loop is fine right

#

its not a serial script btw

#

Although, I do wanna be able to stop it outside of this script

halcyon quartz
#

u mean cancel?

rare harness
#

yes

#

it can cancel a running loop

#

or coroutine.yield also work

halcyon quartz
# rare harness or coroutine.yield also work

local PS = game:GetService("PathfindingService")
local path = PS:CreatePath()
local SCS = game:GetService("ServerScriptService")
local EntityHandler = require(SCS.EntityHandler)

local thread = EntityHandler.EntityStates.Patrol(game.Workspace.NPC1,game.Workspace.NPC1Patrol,path)

task.cancel(thread)

This is the other script, like this?

#

it didn't work

#

by outside of script I mean in another script, not another block of code inside the same script object

rare harness
#

u need to use task.spawn

halcyon quartz
# rare harness u need to use task.spawn

local PS = game:GetService("PathfindingService")
local path = PS:CreatePath()
local SCS = game:GetService("ServerScriptService")
local EntityHandler = require(SCS.EntityHandler)

task.spawn(EntityHandler.EntityStates.Patrol(game.Workspace.NPC1,game.Workspace.NPC1Patrol,path))
task.wait(1)
task.cancel()

idk how to describe what I want to be cancelled

spark gardenBOT
#

studio** You are now Level 12! **studio

halcyon quartz
#

local PS = game:GetService("PathfindingService")
local path = PS:CreatePath()
local SCS = game:GetService("ServerScriptService")
local EntityHandler = require(SCS.EntityHandler)

local thread = task.spawn(function()
EntityHandler.EntityStates.Patrol(game.Workspace.NPC1,game.Workspace.NPC1Patrol,path)
end)
task.wait(1)
task.cancel(thread)

oh like this

#

didn't work

rare harness
#
-- Task/thread
local t = task.spawn(function()
    while true do
        print("Running")
        task.wait(1) -- yields so it can be canceled
    end
end)

task.cancel(t) -- Stops the task at next yield```
halcyon quartz
#

wait

#

it did

#

shoot

#

thanks

#

thats cool how it can access the function inside

halcyon quartz
#

from another script

#

how

#

oh because of my modulescript

#

im dumb

#

listenm its 6 am

#

im not the brightest tool in the shed rn

pallid parrot
next chasm