i’ve made an elevator system where you can join with multiple players at once
and once enough players are inside it starts a timer
the timer relies on heartbeat to count
i’m just wondering on how to fix it cause it creates a heartbeat event when time the timer starts on the elevator and which it might cause some lag
here’s the place
teleport_place.rbxl (80.1 KB)
--here's the code for it
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectionService = game:GetService("CollectionService")
local RunService = game:GetService("RunService")
local SafeTeleport = require(ServerStorage:FindFirstChild("SafeTeleport"))
local Zone = require(ReplicatedStorage:FindFirstChild("Zone"))
local teleportGui = ServerStorage:FindFirstChild("TeleportGui")
local function teleportModule(teleporter)
local zone = Zone.new(teleporter.Zone)
local teleGui = teleportGui:Clone()
teleGui.Parent = teleporter
teleGui.Adornee = teleporter
local frame = teleGui.Frame
local timeText = frame.Time
local playerText = frame.Players
local placeId = teleporter:GetAttribute("PlaceId")
local maxPlayers = teleporter:GetAttribute("MaxPlayers")
local minPlayers = teleporter:GetAttribute("MinPlayers")
local timerValue = teleporter:GetAttribute("Time")
local partyPlayersList = {}
local timerConnect = script.Changed:Connect(function() end) timerConnect:Disconnect()
local debounce
local function updateTimer()
timeText.Text = string.format("Time: %i", teleporter:GetAttribute("Time"))
end
local function updatePlayer()
playerText.Text = string.format("Players: %i", #partyPlayersList)
end
updateTimer()
updatePlayer()
local function timer()
if time() < debounce + 1 then return end --if current time is greater than debounce + 1 second
teleporter:SetAttribute("Time", teleporter:GetAttribute("Time") -1)
updateTimer()
debounce = time()
if teleporter:GetAttribute("Time") > 0 then return end --if time is less or equal to 0
timerConnect:Disconnect()
SafeTeleport(placeId, partyPlayersList) --teleports players
teleporter:SetAttribute("Time", timerValue)
updateTimer()
end
local function addPlayer(Player)
if table.find(partyPlayersList,Player) or #partyPlayersList >= maxPlayers then return end --if player not in part or if party is less than max players
table.insert(partyPlayersList,Player)
updatePlayer()
if #partyPlayersList < minPlayers or timerConnect.Connected then return end --if party is greater or equal to min players or the timer isn't running
debounce = time() timerConnect = RunService.Heartbeat:Connect(timer)
end
local function removePlayer(Player)
table.remove(partyPlayersList, table.find(partyPlayersList, Player))
updatePlayer()
if #partyPlayersList >= minPlayers then return end --if party is less than min players
timerConnect:Disconnect()
teleporter:SetAttribute("Time", timerValue)
updateTimer()
end
zone.playerEntered:Connect(addPlayer)
zone.playerExited:Connect(removePlayer)
end
for _, teleport in pairs(CollectionService:GetTagged("teleporter")) do
teleportModule(teleport)
end