I’m trying to make a rooms entity script, and it works, until you go ~20 rooms away from spawn. Then, it waits for 10-20 seconds at each room door before moving.
I think it has to do with the movetofinished at line 40 but I don’t know what’s wrong with it
game.ReplicatedStorage.PlayerDoorToServer.OnServerEvent:Connect(function(plr, playerdoor)
print(playerdoor)
end)
task.wait(1)
for i, v:Part in pairs(workspace:GetDescendants()) do
if v.Name == "RoomDoor" and v:IsA("Part") then
v.Transparency = 0
v.CanCollide = true
v.Touched:Connect(function(entitycheck)
if entitycheck.Name == "A20Hitbox" and v.Transparency == 0 then
task.wait(0.75)
entitycheck.Parent:Destroy()
end
end)
v.ProximityPrompt.Triggered:Connect(function(sus)
v.ProximityPrompt:Destroy()
print("Door opened")
game.ReplicatedStorage.IncrementEntity:FireClient(sus)
v.Transparency = 0.5
v.CanCollide = false
function spawnEntity()
print("Spawn")
local clone = workspace["A-20"]:Clone()
clone.Parent = workspace
clone:PivotTo(workspace["The Lobby"].EntitySpawn.CFrame)
local a20spawncue = clone.A20Hitbox.A20SpawnCue
local a20entitystatic = clone.A20Hitbox.A20EntitySound
a20spawncue:Play()
a20entitystatic:Play()
for i, room in pairs(_G.rooms_exist) do
clone.Humanoid:MoveTo(room.Position)
clone.Humanoid.MoveToFinished:Wait()
end
end
local WillEntitySpawn = math.random(1, 5)
if WillEntitySpawn == 1 then
spawnEntity()
end
end)
end
end
To me, it looks like it is stopping due to your death. Have you tested hiding in a locker to see how it moves? Or to simply make it so you do not die to see if the player’s death is the issue.
I’ve heard that MoveToFinished can be finicky. You could try this code instead:
local RunService = game:GetService("RunService")
local function waitUntilNear(part, nearTargetPart, nearDistance)
while ((part.Position - nearTarget.Position).Magnitude > nearDistance) and and part:IsDescendantOf(Workspace) and nearTargetPart:IsDescendantOf(Workspace) do
task.wait()
end
end
...
-- (In your other code)
for i, room in pairs(_G.rooms_exist) do
clone.Humanoid:MoveTo(room.Position)
waitUntilNear(clone.PrimaryPart, room, 3)
end
Also note that MoveTo has a 8 second time out (after 8 seconds of moving the humanoid gives up and fires MoveToFinished. So it could also be that.
Here is the code example from the wiki to get around the time out:
local function moveTo(humanoid, targetPoint, andThen)
local targetReached = false
-- listen for the humanoid reaching its target
local connection
connection = humanoid.MoveToFinished:Connect(function(reached)
targetReached = true
connection:Disconnect()
connection = nil
if andThen then
andThen(reached)
end
end)
-- start walking
humanoid:MoveTo(targetPoint)
-- execute on a new thread so as to not yield function
task.spawn(function()
while not targetReached do
-- does the humanoid still exist?
if not (humanoid and humanoid.Parent) then
break
end
-- has the target changed?
if humanoid.WalkToPoint ~= targetPoint then
break
end
-- refresh the timeout
humanoid:MoveTo(targetPoint)
task.wait(6)
end
-- disconnect the connection if it is still connected
if connection then
connection:Disconnect()
connection = nil
end
end)
end
local function andThen(reached)
print((reached and "Destination reached!") or "Failed to reach destination!")
end
moveTo(script.Parent:WaitForChild("Humanoid"), Vector3.new(50, 0, 50), andThen)