#Can i somehow stop the dummy on second click and make him follow the new path?

38 messages · Page 1 of 1 (latest)

hollow heart
#

when i click when hes mid path he does a zigzag patern until he reaches both positions
heres my code:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ClickEvent = ReplicatedStorage:WaitForChild("ClickEvent")
local pfs = game:GetService("PathfindingService")
local human = script.Parent:WaitForChild("Humanoid")
local torso = script.Parent:WaitForChild("Torso")



ClickEvent.OnServerEvent:Connect(function(player, clickPosition, heldItemName)
    print(player.Name .. " clicked at position: " .. tostring(clickPosition) .. " while holding: " .. heldItemName)
    if player.Team and player.Team.Name == "Player 1" and heldItemName == "Hero 1" then
        local path = pfs:CreatePath()
        path:ComputeAsync(torso.Position, clickPosition)
        local waypoints = path:getWaypoints()
        
        for i, waypoint in pairs(waypoints) do
            human:MoveTo(waypoint.Position)
            human.MoveToFinished:Wait(2)
        end
    end
end)


lone cradle
#

if any errors send and ill fix it.

#

And i put some small explanations in the code so your not confused.

dire sirenBOT
#

studio** You are now Level 2! **studio

hollow heart
#

16:49:07.219 StatusChanged is not a valid member of Path "Instance" - Server - DummyScript1:34

lone cradle
#

ohhhhh

hollow heart
#

you sure u changed anything?

dire sirenBOT
#

studio** You are now Level 2! **studio

hollow heart
#

oh wait

#

different error

lone cradle
#

uhmmm

hollow heart
lone cradle
#

man new error every time

#

Please work

hollow heart
lone cradle
#

I give up

hollow heart
#

it feels like the script is running mulpitle times at once in my original script

lone cradle
#

Dont mind these scripts

hollow heart
#

bcuz when i do this

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ClickEvent = ReplicatedStorage:WaitForChild("ClickEvent")
local pfs = game:GetService("PathfindingService")
local human = script.Parent:WaitForChild("Humanoid")
local torso = script.Parent:WaitForChild("Torso")
local waypoints = nil

-- Listen for ClickEvent to break the loop
local function onNewClickEvent()
    waypoints = nil
    
end

ClickEvent.OnServerEvent:Connect(function(player, clickPosition, heldItemName)

    -- Check the player's team and held item
    if player.Team and player.Team.Name == "Player 1" and heldItemName == "Hero 1" then

        -- Create and compute the path
        local path = pfs:CreatePath()
        path:ComputeAsync(torso.Position, clickPosition)
        local waypoints = path:GetWaypoints()

        
        while #waypoints > 0 do
            ClickEvent.OnServerEvent:Connect(onNewClickEvent)
            human:MoveTo(waypoints[1].Position)
            human.MoveToFinished:Wait(2)
            table.remove(waypoints,1)
            print("waypoints left " ..#waypoints)
            wait()

        end
    end
end)

#

and i try clicking mid path

#

this it the output

lone cradle
#

oh

#

Maybe seperate pathfinding function?

#

uhmm

#

i think i know now

#
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ClickEvent = ReplicatedStorage:WaitForChild("ClickEvent")
local pfs = game:GetService("PathfindingService")
local human = script.Parent:WaitForChild("Humanoid")
local torso = script.Parent:WaitForChild("Torso")

local currentPath
local isMoving = false

-- Function to follow a given path
local function followPath(waypoints)
    isMoving = true
    while isMoving and #waypoints > 0 do
        human:MoveTo(waypoints[1].Position)
        human.MoveToFinished:Wait(2)
        if not isMoving then return end  -- Check if movement should be stopped
        table.remove(waypoints, 1)
        print("waypoints left " .. #waypoints)
        wait()
    end
    isMoving = false
end


local function onNewClickEvent()
    isMoving = false  -- Stop the current movement
end

ClickEvent.OnServerEvent:Connect(function(player, clickPosition, heldItemName)
    if player.Team and player.Team.Name == "Player 1" and heldItemName == "Hero 1" then
        -- Stop any current path if a new click occurs
        if isMoving then
            onNewClickEvent()
        end

        -- Create and compute the path
        local path = pfs:CreatePath()
        path:ComputeAsync(torso.Position, clickPosition)
        local waypoints = path:GetWaypoints()

        -- Start following the new path
        followPath(waypoints)
    end
end)
```this should be better now
#

check this i seperated followPath into its own function.

hollow heart
#

can wo hop into vc?

#

it has the same behavior as my original script

lone cradle
hollow heart
#

ok im gonna leave it for tomorow, already spent 4 hours on it today

lone cradle
hollow heart
#

ChatGPT is a king

#

It solved my issue in 30 second that i spent 13 hours on

#
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ClickEvent = ReplicatedStorage:WaitForChild("ClickEvent")
local pfs = game:GetService("PathfindingService")
local human = script.Parent:WaitForChild("Humanoid")
local torso = script.Parent:WaitForChild("Torso")

local shouldBreak = false
local isMoving = false

local function onNewClickEvent()
    shouldBreak = true
end

ClickEvent.OnServerEvent:Connect(function(player, clickPosition, heldItemName)
    local destination = clickPosition

    if isMoving then
        shouldBreak = true
        while isMoving do
            wait() 
        end
    end

    if player.Team and player.Team.Name == "Player 1" and heldItemName == "Hero 1" then
        shouldBreak = false
        isMoving = true

        local path = pfs:CreatePath()
        path:ComputeAsync(torso.Position, clickPosition)
        local waypoints = path:GetWaypoints()

        local eventConnection = ClickEvent.OnServerEvent:Connect(onNewClickEvent)

        while #waypoints > 0 do
            if shouldBreak then
                break
            end
            human:MoveTo(waypoints[1].Position)
            human.MoveToFinished:Wait()
            table.remove(waypoints, 1)
            print("waypoints left " .. #waypoints)
        end

        eventConnection:Disconnect()
        isMoving = false
        shouldBreak = false
    end
end)

fixed code

lone cradle