Well, the title is pretty self-explanatory, but why my touch function is only working once?
Basically i’m making a lobby gate system, but for some reason, when i press the leave button, it only works once, and then i can’t enter the lobby again.
Here’s my code:
Server:
local LobbyEvent = game.ReplicatedStorage.LobbyEvent
local TPS = game:GetService("TeleportService")
local Gate = script.Parent
local BillboardGui = Gate.BillboardGui
local SecondPlaceID = 16185947848
local Countdown = 20
local PlayersInLobby = {}
BillboardGui.Countdown.Text = "Waiting for players..."
function StartCountdown()
repeat
Countdown -= 1
BillboardGui.Countdown.Text = "Starting in: "..Countdown
if #PlayersInLobby == 0 then
BillboardGui.Countdown.Text = "Waiting for players..."
break
end
wait(1)
until Countdown == 0
end
function OnTouched(hit)
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
if Player and not Player:FindFirstChild("IsInLobby") then
local IsInLobbyValue = Instance.new("BoolValue", Player)
IsInLobbyValue.Name = "IsInLobby"
LobbyEvent:FireClient(Player)
--BillboardGui.Countdown.Text = "Starting in: "..Countdown
table.insert(PlayersInLobby, Player)
IsInLobbyValue.Value = true
task.wait(1)
print(PlayersInLobby)
Connection:Disconnect()
StartCountdown()
--TPS:TeleportPartyAsync(SecondPlaceID, PlayersInLobby)
end
end
Connection = Gate.Touched:Connect(function(hit)
OnTouched(hit)
end)
LobbyEvent.OnServerEvent:Connect(function(Player, Value)
for i = 1, #PlayersInLobby do
if PlayersInLobby[i] == Player then
table.remove(PlayersInLobby, i)
print(Player.Name.." has been removed from the party!")
task.wait(1)
print(PlayersInLobby)
end
end
end)
Client:
local LobbyEvent = game.ReplicatedStorage.LobbyEvent
local Player = game.Players.LocalPlayer
local MainLobbyUI = Player.PlayerGui.LobbyMain
local LeaveButton = MainLobbyUI.Leave
LobbyEvent.OnClientEvent:Connect(function()
LeaveButton.Visible = true
LeaveButton.MouseButton1Up:Connect(function()
LobbyEvent:FireServer("LeaveLobby")
Player:FindFirstChild("IsInLobby").Value = false
LeaveButton.Visible = false
end)
end)
You’re not checking the Value of IsInLobby - you’re checking if it exists or not, which is ok but when you leave the lobby and change the value of it to false - it still exists.
Either destroy the instance when you leave or change the check when entering to check the value of IsInLobby instead.
You’re creating a function just to call another function. You can just do: Connection = Gate.Touched:Connect(OnTouched)
because parameters are passed from the event to the provided function.
Still don’t know why, but it’s not working, as you can see, i putted it on the server, look:
LobbyEvent.OnServerEvent:Connect(function(Player, Value)
for i = 1, #PlayersInLobby do
if PlayersInLobby[i] == Player then
table.remove(PlayersInLobby, i)
Player:FindFirstChild("IsInLobby").Value = false
print(Player.Name.." has been removed from the party!")
task.wait(1)
print(PlayersInLobby)
end
end
end)
You’re setting this to false, but when you check for the value, you check whether it exists, not whether it’s false. Change this line to Player:FindFirstChild("IsInLobby"):Destroy()
Even then, that leaves the issue you will have created two values of the same class and with the same name within the player. One might be modified and the other might be checked. Use :Destroy() to get rid of one after it is used.
function OnTouched(hit)
local Player = game.Players:GetPlayerFromCharacter(hit.Parent)
if Player and Player:FindFirstChild("IsInLobby").Value ~= true then
local IsInLobbyValue = Player:FindFirstChild("IsInLobby")
LobbyEvent:FireClient(Player)
--BillboardGui.Countdown.Text = "Starting in: "..Countdown
table.insert(PlayersInLobby, Player)
IsInLobbyValue.Value = true
task.wait(1)
print(PlayersInLobby)
Connection:Disconnect()
StartCountdown()
--TPS:TeleportPartyAsync(SecondPlaceID, PlayersInLobby)
end
end