For loop stops when player respawns

  1. Im trying to make the player move using waypoints.

  2. But when the player dies, the for loop stops when the player respawns.

  3. I tried putting print() function but it dosen’t show it only shows outside of the for loop.

Video:
robloxapp-20230826-1431213.wmv (216.3 KB)

Pictures:


picture2
note: the script above the walk script is the death effect

Code:


local waypoints = workspace.WaypOints
local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local humanoid = char:FindFirstChild("Humanoid")

if char ~= nil then
for waypoint = 1, #waypoints:GetChildren() do
	humanoid:MoveTo(waypoints:WaitForChild(waypoint).Position)
	humanoid.MoveToFinished:Wait()
	end
else
	return
end

If you can help me, thank you

I mean you can do:

local Players = game:GetService("Players")

local LocalPlayer = Players.LocalPlayer

local WayPoints = workspace:FindFirstChild("WaypOints") -- I think you might've misspelt WayPoints?

local function moveToWaypoints(humanoid: Humanoid)
    for _, waypoint in pairs(WayPoints:GetChildren()) do
        if humanoid then
            humanoid:MoveTo(waypoint.Position)
            humanoid.MoveToFinished:Wait()
        else
            return
        end
    end
end

local function handleWaypoints()
    local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
    local humanoid = Character:FindFirstChild("Humanoid"):: Humanoid

    if humanoid then
        moveToWaypoints(humanoid)
    end
end

local function onCharacterAdded(character)
    local humanoid = character:WaitForChild("Humanoid"):: Humanoid

    humanoid.Died:Connect(function()
        handleWaypoints()
    end)
end

LocalPlayer.CharacterAdded:Connect(onCharacterAdded)
handleWaypoints()

Let me know how it goes!

I tried but it dosen’t work when it still respawns?

I might’ve just written it wrong then since I was rushing, take your original code and just make it run with a Humanoid.Died event so it’ll run again or connect it to a CharacterAdded event. The latter is probably better.

i added some changes:


local Players = game:GetService("Players")

local LocalPlayer = Players.LocalPlayer

local WayPoints = workspace:FindFirstChild("WaypOints") -- I think you might've misspelt WayPoints?

local function moveToWaypoints(humanoid: Humanoid)
	for waypoint = 1, #WayPoints:GetChildren() do
		if humanoid then
			humanoid:MoveTo(WayPoints:FindFirstChild(waypoint).Position)
			humanoid.MoveToFinished:Wait()
		else
			return
		end
	end
end

local function handleWaypoints()
	local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
	local humanoid = Character:FindFirstChild("Humanoid"):: Humanoid

	if humanoid then
		moveToWaypoints(humanoid)
	end
end

local function onCharacterAdded(character)
	local humanoid = character:WaitForChild("Humanoid"):: Humanoid

	humanoid.Died:Connect(function()
		handleWaypoints()
	end)
end

LocalPlayer.CharacterAdded:Connect(onCharacterAdded)
handleWaypoints()

I fixed the problem, its just that the waypoints were unanchored but thanks for helping.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.