Why will proximity prompts not work after a script runs?

For some reason after a player leaves in a lobby (has an array) the proximity prompts I use to let players enter an exit don’t work, I’ve tried to see if the array is affected but it doesn’t let me. What happens is when someone leaves in the lobby everything happens like I want it but, players wont get teleported to the waiting room. It partially works because the proximity prompt changes to leave once the enter one is activated but the player isn’t teleported.

This is my code:

local entered = {}
local gui = game.Workspace.Exit.gui.BillboardGui.Frame
local RemoteEvent = game.ReplicatedStorage.Lobby
local timer = Instance.new("NumberValue")
timer.Value = 10
local vtimer = gui.timer
local teleporting = Instance.new("BoolValue")
teleporting.Value = false
local vteleporting = gui.status
vteleporting.Text = " "
local TeleportService = game:GetService("TeleportService")
local placeid = 8964283433
if not game:GetService("RunService"):IsStudio() then
	local ReservedServerCode = TeleportService:ReserveServer(placeid)
end

game.Players.PlayerRemoving:Connect(function(player)
	if table.find(entered, player) then
		table.remove(entered, table.find(entered, player))
	end
end)

timer:GetPropertyChangedSignal("Value"):Connect(function()
	if timer.Value < 1 then
		teleporting.Value = true
		vteleporting.Text = "Teleporting..."
	else
		teleporting.Value = false
		vteleporting.Text = " "
	end
end)

teleporting:GetPropertyChangedSignal("Value"):Connect(function()
	if teleporting.Value == true then
		if not game:GetService("RunService"):IsStudio() then
			TeleportService:TeleportToPrivateServer(placeid, ReservedServerCode, entered)
			entered = {}
			gui.players.Text = "Players: " ..#entered
		else
			print("Teleporting")
		end
	end
end)	

RemoteEvent.OnServerEvent:Connect(function(player, request)
	if request == "Enter" then
		player.Character.HumanoidRootPart.CFrame = game.Workspace.Exit.gui.CFrame
		table.insert(entered, player)
	else
		player.Character.HumanoidRootPart.CFrame = game.Workspace.leavepart.CFrame
		for index = #entered, 1, -1 do
			if entered[index] == player then
				table.remove(entered, index)
			end
		end
		
	end
	gui.players.Text = "Players: "..#entered
end)

while true do
	task.wait()
	if #entered > 0 then
		while timer.Value > 0 and #entered > 0 do
			task.wait(1)
			timer.Value -= 1
			vtimer.Text = "Timer: " .. timer.Value
		end
	else
		timer.Value = 10
		vtimer.Text = "Timer: " .. timer.Value
	end
end

And this is the code for the client sided script for the prompts. (In stater player scripts):

local RemoteEvent = game.ReplicatedStorage.Lobby
local enterprompt = game.Workspace.Walls["lobby wall"].Enterlobby
local leaveprompt = game.Workspace.Walls["lobby wall"].Leavelobby

enterprompt.Triggered:Connect(function(Player)
	enterprompt.Enabled = false
	leaveprompt.Enabled = true
	RemoteEvent:FireServer("Enter")
end)

leaveprompt.Triggered:Connect(function(Player)
	enterprompt.Enabled = true
	leaveprompt.Enabled = false
	RemoteEvent:FireServer("Leave")
end)

I don’t know why it breaks, please help.

maybe it’s bc Triggered works only w server

The prompt works fine until someone in the lobby leaves.

teleport each individual player and wrap it in a pcall (easy mode real)

1 Like

That doesn’t work and I already teleport each individual player.

Edit: Looking at it again, the player isn’t added to the arrays so the remote event probably doesn’t fire but I’m not sure.

use the leave prompt to remove the player, i don’t really know because how you wrote it is weird and im trying to figure it out myself

If it receives any message but “Enter” then it will remove the player, “Enter” is sent when the enter prompt is activated.

i meant that is it not written well so i cant understand how to fix it
i genuinely just think the best way to go about this is to rewrite it

I don’t know a lot about scripting so that would be difficult.

When I started reading the first code, I already noticed an error:

if not game:GetService("RunService"):IsStudio() then
	local ReservedServerCode = TeleportService:ReserveServer(placeid)
end

It seems like you’re confused with global and local variables.


First, let’s start with Scope. According to the Roblox Documentation, the scope of a variable or function is the block of code that can access it, and it can be global or local. A block of code is the body of a control structure or function. All blocks can access global variables and functions. A block can access local variables and functions in its parent block, but not in any of its child blocks. i just copy-pasted this from the docs lol

What this basically means is that, you see those lines in the left side of the code when you script? One line represents the height of a block of code. A block of code can be a control structure (loops or if else then statements) or functions.

Scope basically asks this question: Can a block of code access this variable? Can other blocks access it too? By default, variables have a global scope. A variable is a representation of any value (strings, numbers, tables, instances, and even functions!). If a variable has a local before it, as the name says, it becomes a local variable. However, local variables are faster than global variables, so it’s recommended to use local variables instead.

Global Variables vs. Local Variables

  • Global Variables - these variables can be accessed by every code block in the code, meaning it can be accessed anywhere in the code, even before the line where the variable is created! (though you should be careful when doing that); doesn’t have a local before it; slower than local variables

  • Local Variables - these variables can only be accessed on the code block where it was created and only after the line where it was created; always have a local before it; faster than global variables


Now that we know what the difference between global and local variables is, we can now solve the problem.

The reason why it wouldn’t work because, since it was created in the code block if not game:GetService("RunService"):IsStudio() then, it can only be used in that code block and not outside that code block. To fix this, we either make it global (removing the local) or keep it as local but make it accessible to every code block. Because it’s recommended to use local variables, we use the second option instead.

How will we do this? We simply just move it outside the code block (make sure it’s before any code block) and define it as an empty variable (nil value).

local TeleportService = game:GetService("TeleportService")
local placeid = 8964283433
local ReservedServerCode -- or `local ReservedServerCode = nil`
if not game:GetService("RunService"):IsStudio() then
	
end

And lastly, we define ReservedServerCode in that code block. Make sure there’s no local before it because, sure, the variable will change, but only for that code block because of slope.

local TeleportService = game:GetService("TeleportService")
local placeid = 8964283433
local ReservedServerCode
if not game:GetService("RunService"):IsStudio() then
	ReservedServerCode = TeleportService:ReserveServer(placeid)
end

And you’re done! The final code should look like this.

local entered = {}
local gui = game.Workspace.Exit.gui.BillboardGui.Frame
local RemoteEvent = game.ReplicatedStorage.Lobby
local timer = Instance.new("NumberValue")
timer.Value = 10
local vtimer = gui.timer
local teleporting = Instance.new("BoolValue")
teleporting.Value = false
local vteleporting = gui.status
vteleporting.Text = " "
local TeleportService = game:GetService("TeleportService")
local placeid = 8964283433
local ReservedServerCode
if not game:GetService("RunService"):IsStudio() then
	ReservedServerCode = TeleportService:ReserveServer(placeid)
end

game.Players.PlayerRemoving:Connect(function(player)
	if table.find(entered, player) then
		table.remove(entered, table.find(entered, player))
	end
end)

timer:GetPropertyChangedSignal("Value"):Connect(function()
	if timer.Value < 1 then
		teleporting.Value = true
		vteleporting.Text = "Teleporting..."
	else
		teleporting.Value = false
		vteleporting.Text = " "
	end
end)

teleporting:GetPropertyChangedSignal("Value"):Connect(function()
	if teleporting.Value == true then
		if not game:GetService("RunService"):IsStudio() then
			TeleportService:TeleportToPrivateServer(placeid, ReservedServerCode, entered)
			entered = {}
			gui.players.Text = "Players: " ..#entered
		else
			print("Teleporting")
		end
	end
end)	

RemoteEvent.OnServerEvent:Connect(function(player, request)
	if request == "Enter" then
		player.Character.HumanoidRootPart.CFrame = game.Workspace.Exit.gui.CFrame
		table.insert(entered, player)
	else
		player.Character.HumanoidRootPart.CFrame = game.Workspace.leavepart.CFrame
		for index = #entered, 1, -1 do
			if entered[index] == player then
				table.remove(entered, index)
			end
		end
		
	end
	gui.players.Text = "Players: "..#entered
end)

while true do
	task.wait()
	if #entered > 0 then
		while timer.Value > 0 and #entered > 0 do
			task.wait(1)
			timer.Value -= 1
			vtimer.Text = "Timer: " .. timer.Value
		end
	else
		timer.Value = 10
		vtimer.Text = "Timer: " .. timer.Value
	end
end

If there are more problems, feel free to ask me and others too. Have a good day!

1 Like

That doesn’t work, the error is that after the player leaves the code for the player entering the array (and the waiting room) doesn’t activate.

Can you please clarify this? It’s better if you’re able to send a video recording. Thank you

are you testing in studio or in game?

Studio, I don’t know how to import a video, especially of multiple tabs.

Should I try in normal game instead of studio?

I dont quite understand the issue… you are trying to test teleporting in studio?..
Teleport

image

No, the parameters around the reserved server code are meant to make it not break as soon as it runs in studio. I’m testing it by having 1 player enter lobby and leave then the other player tries to enter.

I tested your code and works fine for me, the only big issue I found is the creation of the ReservedServerCode variable, which was local to a function.

I tested it in real game and I got teleported correctly. I tried the feature to Enter and wait for the teleport and did work, and I tried by using Leave while waiting and the timer did stop correctly, I tried again Enter and timer started again and at the end with a successful teleport.

I dont see the issue

Your code doesnt reset the table in studio. It might not be the full problem but it should make it easier to solve.

1 Like

I have a video of what happens but I don’t know how to import it.

1 Like