Touch function only working once?

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)

Any help would be aprecciated!

1 Like

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.

Oh, thanks for helping, now i understand, let me see if it works. If it does, i will mark you as solution
Edit: Nope, it still does not work

Just another little side note here:

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.

Yep, still not working, i don’t know why tbh

If you’re changing this on the Client then the server won’t see it. You need to change it on the Server

Oh wait a second, lemme try it again

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()

I already changed it:

Player:FindFirstChild(“IsInLobby”).Value ~= true

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.

:confused:

local IsInLobbyValue = Player:FindFirstChild(“IsInLobby”)

I’ve got no updated code to look at, so I don’t know what you’ve changed and what you haven’t. Can you send the updated code please?

1 Like

Sure, here’s it

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
1 Like

Why are you disconnecting that? You need it to register future part touches.

wait…I’ve just noticed this:

After touching it the first time your disconnecting the event - so it can’t detect any more touches.

Edit: 12345koip beat me to it.

1 Like

For performance, idk if this will help or not

No, you need it to detect future touches. This might be why it isn’t working.

1 Like

Probably, let’s see now

IT’S WORKING!!

1 Like

yeah, he beated you lol

man, i don’t like that limit word