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:IsDescendantOf(Workspace) and nearTargetPart:IsDescendantOf(Workspace) and ((part.Position - nearTarget.Position).Magnitude > nearDistance) 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)
Here is a part of the script I use for my doors game so that the entity moves smoothly and doesn’t require a humanoid. I send it my entity/enemy in this case I called him husk and I send it the entrance part, looped through a table of waypoints inside of the room model and then sent it the exit part. Efficiently moving him through the room.
function husk.LerpTo(entity, target)
local alpha = 0
local speed = 100
local distance = (entity.Position - target.Position).Magnitude
local relativeSpeed = distance / speed
local startCFrame = entity.CFrame
local loop = nil
local reachedTarget = Instance.new("BindableEvent")
loop = RunService.Heartbeat:Connect(function(deltaTime)
husk.FindPlayers(entity)
local goalCFrame = startCFrame:Lerp(target.CFrame, alpha)
entity:PivotTo(goalCFrame)
alpha += deltaTime / relativeSpeed
if alpha >= 1 then
loop:Disconnect()
reachedTarget:Fire()
end
end)
reachedTarget.Event:Wait()
end
part should be a part of what’s being moved towards the target part. nearDistance is how close is close enough to be finished (e.g. until part is 3 studs away from nearTargetPart, until it’s 10 studs away, etc)
For some reason I just get the error “ServerScriptService.DoorMasterScript2:40: attempt to index nil with ‘Position’” which is weird because the model does have a primarypart