I am currently making a Rooms game. You know, the moving faces going throughout the rooms. Currently, I’ve successfully made generating rooms, lockers and more. Now that I’ve moved on to the Entities, that was when things got pretty tricky.
You see, I’ve managed to make my entity move through the rooms like this.
(Don’t mind the floating chairs lol)
However, a problem arises. If you open a door while the entity is active, then you would be safe from it since it stops at the last door like this.
I tried using while loops, but it broke the code even more.
I need some help fixing this bug, because I lowkey do not have the brains to solve this by myself
Any help will be appreciated!!
Here is the code for the entity handler
-- local model = script.Parent.Parent
local mainPart = model.MainPart
local Runservice = game:GetService("RunService")
local rooms = workspace.LoadedRooms
for _, value in ipairs(model:GetDescendants()) do
if value:IsA("Sound") then
value:Play()
end
end
local function lerpTo(model:Model, target:BasePart)
local alpha = 0
local speed = 150
local distance = (mainPart.Position - target.Position).Magnitude
local ReletiveSpeed = distance / speed
local StartCFrame = model.PrimaryPart.CFrame
local loop = nil
local reachedTarget = Instance.new("BindableEvent")
loop = Runservice.Heartbeat:Connect(function(deltaTime)
local goalCFrame = StartCFrame:Lerp(target.CFrame, alpha)
model:PivotTo(goalCFrame)
alpha += deltaTime / ReletiveSpeed
if alpha >= 1 then
loop:Disconnect()
reachedTarget:Fire()
end
end)
reachedTarget.Event:Wait()
end
local function Navigate(model)
for i = 1, #rooms:GetChildren() do
local room = rooms:GetChildren()[i]
lerpTo(model, room.Enter)
local waypoints = room:FindFirstChild("Waypoints")
if waypoints then
for i = 1, #waypoints:GetChildren() do
lerpTo(model, waypoints:GetChildren()[i])
end
end
lerpTo(model, room.Exit)
end
end
Navigate(model)
model:Destroy()
Issue is because your script does not account for rooms that are created during the entity’s movement.
Here’s updated code that will account for new rooms:
local model = script.Parent.Parent
local mainPart = model.MainPart
local Runservice = game:GetService("RunService")
local rooms = workspace.LoadedRooms
for _, value in ipairs(model:GetDescendants()) do
if value:IsA("Sound") then
value:Play()
end
end
local function lerpTo(model:Model, target:BasePart)
local alpha = 0
local speed = 150
local distance = (mainPart.Position - target.Position).Magnitude
local ReletiveSpeed = distance / speed
local StartCFrame = model.PrimaryPart.CFrame
local loop = nil
local reachedTarget = Instance.new("BindableEvent")
loop = Runservice.Heartbeat:Connect(function(deltaTime)
local goalCFrame = StartCFrame:Lerp(target.CFrame, alpha)
model:PivotTo(goalCFrame)
alpha += deltaTime / ReletiveSpeed
if alpha >= 1 then
loop:Disconnect()
reachedTarget:Fire()
end
end)
reachedTarget.Event:Wait()
end
local function Navigate(model)
local index = 0
--Keep travelling until this is last room
while index < #rooms:GetChildren() do
--Index to next room
index += 1
--Get the room model
local room = rooms:GetChildren()[index]
--Move to its entrance
lerpTo(model, room.Enter)
--Travel along its waypoints if it has any
local waypoints = room:FindFirstChild("Waypoints")
if waypoints then
for i = 1, #waypoints:GetChildren() do
lerpTo(model, waypoints:GetChildren()[i])
end
end
--Then finally, to the exit of the room
lerpTo(model, room.Exit)
end
--[[This ensures that we end at the last room, even if
a room is created while the entity is travelling]]
end
Navigate(model)
model:Destroy()
IMPORTANT: If a room is destroyed while the entity is travelling, the script will end the movement one room early. To fix this, you could make index a global variable, and reduce it by 1 when rooms.ChildRemoved fires.
local model = script.Parent.Parent
local mainPart = model.MainPart
local Runservice = game:GetService("RunService")
local rooms = workspace.LoadedRooms
for _, value in ipairs(model:GetDescendants()) do
if value:IsA("Sound") then
value:Play()
end
end
local function lerpTo(model:Model, target:BasePart)
local alpha = 0
local speed = 150
local distance = (mainPart.Position - target.Position).Magnitude
local ReletiveSpeed = distance / speed
local StartCFrame = model.PrimaryPart.CFrame
local loop = nil
local reachedTarget = Instance.new("BindableEvent")
loop = Runservice.Heartbeat:Connect(function(deltaTime)
local goalCFrame = StartCFrame:Lerp(target.CFrame, alpha)
model:PivotTo(goalCFrame)
alpha += deltaTime / ReletiveSpeed
if alpha >= 1 then
loop:Disconnect()
reachedTarget:Fire()
end
end)
reachedTarget.Event:Wait()
end
local function Navigate(model)
local index = 0
--Keep travelling until this is last room
while index < #rooms:GetChildren() do
--Index to next room
index += 1
--Get the room model
local room = rooms:GetChildren()[index]
--Move to its entrance
lerpTo(model, room.Enter)
--Travel along its waypoints if it has any
local waypoints = room:FindFirstChild("Waypoints")
if waypoints then
for i = 1, #waypoints:GetChildren() do
lerpTo(model, waypoints:GetChildren()[i])
end
end
--Then finally, to the exit of the room
lerpTo(model, room.Exit)
end
--[[This ensures that we end at the last room, even if
a room is created while the entity is travelling]]
end
Navigate(model)
model:Destroy()