Enter script and Exit script will teleport players at door to inside or outside of a building by checking substring of model’s name.
Each buildings’ models is separated into exterior and interior like this.
Exter_building1, Inter_building1
So 1 building has 2 models, and I link them using module scripts.
Inside these ex-in there is a part called door, work as a player detector.
Door has Target and Looker to adjust look direction and teleport player.
Enter and Exit are server script.
They are almost the same just swap inter/exter and outside/inside.
Below is Enter script
local event = game.ReplicatedStorage:WaitForChild("Transition")
local manager = require(game.ServerScriptService.enter_exit_manager)
local exter = script.Parent
local door = exter.door
local inter = manager.getinterfromexter(exter) --return interior model of the same building
local inside = inter.door.Looker -- use to locate teleport position
local lookat = inter.door.Target -- use to adjust direction a player is facing
local ovl = OverlapParams.new()
ovl.FilterType = Enum.RaycastFilterType.Include
while task.wait() do
ovl.FilterDescendantsInstances = require(game.ServerScriptService.players_list) -- get player list
local parts = workspace:GetPartsInPart(door, ovl)
for i, part in ipairs(parts) do
local character = nil
if part.Parent:FindFirstChild("Humanoid") then
character = part.Parent
if character and character.Humanoid.Health ~= 0 then
local player = game.Players:GetPlayerFromCharacter(character)
if player then
player.Character.Humanoid.WalkSpeed = 0
event:FireClient(player, inside, lookat) -- screen transition effect
task.wait(1.5)
character.HumanoidRootPart.CFrame = CFrame.lookAt(inside.Position, lookat.Position) -- teleport and adjust position
task.wait(1.5)
player.Character.Humanoid.WalkSpeed = 16
end
end
end
end
end
and this is module script linking ex-in.
local building_manager = {}
local b_in = require(script.inter) -- list of interior model
local b_ex = require(script.exter) -- list of exterior model
building_manager.inter = b_in
building_manager.exter = b_ex
--these 2 functions below use to link ex-in
function building_manager.getinterfromexter(exter)
local exist = table.find(building_manager.exter, exter)
if exist then
for i, inter in pairs(building_manager.inter) do
if inter.Name:sub(6, -1) == exter.Name:sub(6,-1) then
return inter
end
end
end
end
function building_manager.getexterfrominter(inter)
local exist = table.find(building_manager.inter, inter)
if exist then
for i, exter in pairs(building_manager.exter) do
if exter.Name:sub(6, -1) == inter.Name:sub(6,-1) then
return exter
end
end
end
end
return building_manager
I want to know that is there anything to watch out or does it need to be optimized?
Any other kind of feedbacks are also welcome.