So I have a script that when ever a player touches the elevator he will be teleported into the elevator with a debounce that is also activated until the player clicks an exit button. When there are enough players in the elevator there will be a countdown that counts to 0. This works fine until you quickly click the exit button and re enter the elevator since then the countdown will go way faster then normal.
I have no clue on how to fix this, and tried to figure out what possibly the problem could be with prints, and just changing the code in general, but nothing worked so far.
--server script--
for Index, GameStarter in pairs(CollectionService:GetTagged("GameStarter")) do
local Elevator = ElevatorManager.ElevatorSetUp(GameStarter)
print(Elevator)
for Index, TouchObject in pairs(GameStarter:GetDescendants()) do
if TouchObject:IsA("BasePart") and TouchObject.Name == "TouchPart" then
TouchObject.Touched:Connect(function(Hit)
local humanoid = Hit.Parent:FindFirstChild("Humanoid")
if humanoid then
local Player = game.Players:GetPlayerFromCharacter(Hit.Parent)
local PlayerInMatchMaking = Player:GetAttribute("PlayerInMatchMaking")
if PlayerInMatchMaking == false then
print("playerInteractTime")
Player:SetAttribute("PlayerInMatchMaking", true)
Elevator:PlayerInteraction(Player)
end
end
end)
end
end
end
--Module Script--
local ElevatorManager = {}
ElevatorManager.__index = ElevatorManager
local Rep = game:GetService("ReplicatedStorage")
function ElevatorManager.ElevatorSetUp(GameStarter)
local self = setmetatable({}, ElevatorManager)
self.ElevatorObj = GameStarter
self.MaxPlayers = self.ElevatorObj:GetAttribute("MaxPlayerAmount")
self.CurrentPlayers = self.ElevatorObj:GetAttribute("CurrentPlayerAmount")
self.GameStarting = self.ElevatorObj:GetAttribute("GameIsAboutToStart")
self.CountDown = self.ElevatorObj:GetAttribute("CountDownBeforeStart")
self.TextLabel = GameStarter.Main.BillboardGui.TextLabel
self.Team = {}
self.TextLabel.Text = self.CurrentPlayers.."/"..self.MaxPlayers.." Players"
return self
end
function ElevatorManager:PlayerInteraction(Player)
if self.CurrentPlayers <= self.MaxPlayers then
self.CurrentPlayers = self.CurrentPlayers +1
self.TextLabel.Text = self.CurrentPlayers.."/"..self.MaxPlayers.." Players"
self.Team[Player] = Player
-- print("Interactor")
if self.CurrentPlayers == self.MaxPlayers then
Rep.ElevatorAddedPlayer:FireClient(Player)
self:PrepareStart()
end
end
end
function ElevatorManager:PrepareStart()
--print("t")
local FireCoroutine = coroutine.wrap(function()
while self.CurrentPlayers == self.MaxPlayers do
self.CountDown = self.CountDown -1
self.TextLabel.Text = self.CountDown
wait(1)
if self.CountDown <= 0 then
break
end
end
end)
FireCoroutine()
Rep.ElevatorRemovedPlayer.OnServerEvent:Connect(function(Player)
if self.Team[Player] then
self.CountDown = self.ElevatorObj:GetAttribute("CountDownBeforeStart")
self.CurrentPlayers = self.CurrentPlayers -1
self.TextLabel.Text = self.CurrentPlayers.."/"..self.MaxPlayers.." Players"
self.Team[Player] = nil
Player:SetAttribute("PlayerInMatchMaking", false)
end
end)
end
return ElevatorManager