Hi, i have problem with td elevators. Script works for one elevator. But if there is more. It has problem with leaving. becouse it only reads the last one in for loop. Do i have to copy script and paste it to every elevator? Or is there any more efficient way?
Create some elevator manager.
Best is ServerScriptService, where you create a script named ElevatorManager, which takes all existing elevators, and registers listener for every one of them if player steps on any of them.
If player steps on any of these elevators, a countdown will start for such elevator, which gives time for other players to step on same elevator until the countdown is done.
-- you should have a folder named "Elevators", and every model in this folder should be an elevator
local elevators = game.Workspace.Elevators:GetChildren()
-- In case your folder has something else other than elevators, cherry-pick them using a loop
for i, e in ipairs(elevators) do
-- Every elevator should have a part which will behave as a "door" for player
-- So if player touches such door, he will be taken as a player in this elevator
local door = e.Door
door.Touched:Connect(function(part)
-- Create some function which will determine, if touched part is player's part
local player = determinePartIsPlayer(part)
-- Check if touched part is valid player and check if player is not already in any elevator
if (player ~= nil or playerIsInAnyElevator(player)) then
return
end
-- If everything is alright, set player as a waiting player on elevator
-- It's important to specify what elevator the player is in
-- Since this will define what players should be teleported into what map
joinPlayer(e, player)
end)
end
I don’t know if this lua code is written correctly, since I usually use Typescript for Roblox dev, however this short part of code could give you some good starting point on how to manage multiple elevators and players in them.
leaving train is a bigger problem for me, becouse if you will make a remote event in for loop it will only activate for the last in loop.
In that case, try to initialize a reference variable in the loop body. Ref addresses are kept even in loop’s value is on something else.
for i,v in ipairs(...) do
local ref = v
button.Activated:Connect(function()
ref.SomeChange = SomeValue
end)
end
I tried that… and it didnt work. becouse remote event only fires on the last elevator. Not on every.
Better show the code so I can coordinate you on the right path.