Horror Lobby teleporter issue

Hello! I am new to scripting about 2-3 months in, i am sorry if this is not in the correct topic! Also this is my first topic ever.

  1. What do you want to achieve? Keep it simple and clear!
    When a player touches the TeleportPart they get teleported inside and a Value gets added to a Folder, when they press leave while still touching the TeleportPart it teleports them to the TeleportOutPart and deletes that Value from the folder.
  2. What is the issue? Include screenshots / videos if possible!
    When touching the TouchPart and triggering the RemoteEvent which activates once you press the “Leave Button” at the same time it removes the “Value” from the folder however because i am touching the TouchPart at the same time it adds the “Value” back once it has been removed and it does NOT teleport the player to the “TeleportOut” part, instead it teleports BACK to the TeleportPart

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    i tried changing the debounce location, adding checks for when the player is inside etc… but maybe i didn’t do it well
local PlaceId = place id 
local TeleportService = game:GetService('TeleportService')
local Players = game:GetService('Players')
local Folder = script.Parent:WaitForChild('Players')
local Debouncer = false
local PlayerCount = script.Parent:WaitForChild("PlayerCount"):WaitForChild("TextLabel")
local TextLabel = script.Parent:WaitForChild("BillboardGui"):WaitForChild("TextLabel")
local Ready = script.Parent:WaitForChild("Ready")
local Teleporting = script.Parent:WaitForChild("Teleporting")
local MaxPlayers = 2
local MinPlayers = 1
local TeleportPart = script.Parent:WaitForChild("TeleportPart")
local TeleportOutPart = workspace:WaitForChild("Teleporters"):WaitForChild("TeleportOut")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remote1 = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("JoinLobby")
local Remote2 = ReplicatedStorage:WaitForChild("Remotes"):WaitForChild("LeaveLobby")
local timer = 20


function checkIfExist(Character)
	for _, value in pairs(Folder:GetChildren()) do
		if value.Value == Character then
			return true
		end
	end
	return false
end

function TeleportParty()
	local Plrs = {}
	for _, value in pairs(Folder:GetChildren())  do
		if value  then
			table.insert(Plrs, Players:GetPlayerFromCharacter(value.Value))
		end
	end
	local ReserveCode = TeleportService:ReserveServer(PlaceId)
	TeleportService:TeleportToPrivateServer(PlaceId,ReserveCode,Plrs)
	wait(5)
	Folder:ClearAllChildren()
end

function UpdateGui()  
	PlayerCount.Text = tostring(#Folder:GetChildren())..'/'..tostring(MaxPlayers) 
end

script.Parent.Touched:Connect(function(part)
	if not Debouncer then
		Debouncer = true 
		if part.Parent:FindFirstChild("Humanoid") and #Folder:GetChildren() <= MaxPlayers then
			if not checkIfExist(part.Parent) then
				if not Teleporting.Enabled then
					local Tag = Instance.new('ObjectValue')
					Tag.Value = part.Parent
					Tag.Parent = Folder
					part.Parent:SetPrimaryPartCFrame(TeleportPart.CFrame)
					Remote1:FireClient(Players:GetPlayerFromCharacter(part.Parent))
					print("Players teleported")
				elseif Teleporting.Enabled == true then
					print("Can't enter players are being teleported!")
				end

			end  
		elseif #Folder:GetChildren() >= MaxPlayers then
			print("Player limit reached")
		end
		Debouncer = false
	end    
end)

Remote2.OnServerEvent:Connect(function(player)
	for _,value in pairs(Folder:GetChildren()) do
		if value.Value ==  player.Character then
			value:Destroy()
			player.Character:SetPrimaryPartCFrame(TeleportOutPart.CFrame)
		end    
	end
end)

local characters = {} 
Players.PlayerAdded:Connect(function(player)
	player.CharacterRemoving:Connect(function(character)
		characters[player] = character
	end)
end)

Players.PlayerRemoving:Connect(function(player)
	local character = characters[player]

	if character then
		for _, value in pairs(Folder:GetChildren()) do
			if character == value.Value then
				value:Destroy()
				print("Value has been deleted")
			else
				print("Value still standing")
			end
		end

		characters[player] = nil 
	end
end)

Folder.ChildAdded:Connect(UpdateGui)
Folder.ChildRemoved:Connect(UpdateGui)




while true do
	wait(1)
	for count = timer, 0, -1 do
		wait(1)
		TextLabel.Text = count
	end
	local playerCount = #Folder:GetChildren()
	if #Folder:GetChildren() >= MinPlayers and #Folder:GetChildren() <= MaxPlayers then

		Ready.Enabled = false
		Teleporting.Enabled = true
		print("Teleporting players...")
		TeleportParty()
		wait()
		while #Folder:GetChildren() > 0 do

			print("Waiting to teleport players...")
		end
		wait(1)
		Ready.Enabled = true
		Teleporting.Enabled = false
		print("Teleportation completed, restarting countdown.")

	end
end

I do not take credit for the script as i followed a tutorial
tutorial link : tutorial
Also i am sorry if i over-complicated it, i am not too good at explaining things, thanks in advance!

2 Likes

This happens because you’re clicking the leave button and touching the part which instantly adds the value again. A solution that I thought of would be first teleporting the player out of there and after that deleting the value instead of doing it before teleporting the player.

Like this:

Remote2.OnServerEvent:Connect(function(player)
	for _,value in pairs(Folder:GetChildren()) do
		if value.Value ==  player.Character then
			player.Character:SetPrimaryPartCFrame(TeleportOutPart.CFrame)
			value:Destroy()
		end    
	end
end)
1 Like

Alright, i tried moving the player out first and then destroying the value (with the script you provided) however, the issue still persists.

1 Like

Then add a task.wait(1) after teleporting the player out and before destroying the value, this should add a delay.

2 Likes

like this?

Remote2.OnServerEvent:Connect(function(player)
	for _,value in pairs(Folder:GetChildren()) do
		if value.Value ==  player.Character then
			player.Character:SetPrimaryPartCFrame(TeleportOutPart.CFrame)
			task.wait(1)
			value:Destroy()
		end    
	end
end)
1 Like

That script should work fine, if it doesn’t, lmk.

1 Like

You didn’t add a wait() statement after running the code, then setting the Debouncer to false again. The code executes so fast that by the time you are leaving, the Debouncer has already been set to false again. Try adding this:

task.wait(1)
Debouncer = false
1 Like

That could be the issue too but in the script.Parent.Touched event he also checks if the Value exists so it would probably have to do with the Touched event firing really fast that it doesn’t give time to teleport the player outside before deleting the value.

As both versions work, can i put you both as the “solution” or can i put only one?

I am not sure lol, I think you can only mark one as the solution.

mark whichever one you think was the most relevant as the solution i guess, im not really sure

Alright then, i’ll mark yours as you were the first to give it.
I am really grateful to @12345koip too, thanks guys!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.