Proximity prompt response on client and server

I’m making a game and I have a lobby system before a boss. I’m currently working on entering and leaving and I’m using a proximity prompt for this. This is the current code.

local enterprompt = game.Workspace.Walls["lobby wall"].Enterlobby
local leaveprompt = game.Workspace.Walls["lobby wall"].Leavelobby
local entered = {}
local gui = game.Workspace.Exit.gui.BillboardGui.Frame

enterprompt.Triggered:Connect(function(Player)
	enterprompt.Enabled = false
	leaveprompt.Enabled = true
	Player.Character.HumanoidRootPart.CFrame = game.Workspace.Exit.gui.CFrame
	table.insert(entered, Player)
	gui.players.Text = "Players: "..#entered
end)

leaveprompt.Triggered:Connect(function(Player)
	enterprompt.Enabled = true
	leaveprompt.Enabled = false
	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
	gui.players.Text = "Players: "..#entered
end)

It works in single player but if you try it in multiplayer then the leave prompt will show up for all the players when one person enters it, I think all I need to do somehow is transfer the input to a client response with the array and teleport on the server but I don’t know how to do that. (The gui part is some lobby things like players and timer.)

My suggestion is to just clone the proximity prompts on the client so each individual client has their own pair of them. This will make sure that if a player triggers the enter prompt, their own pair of prompts will be enabled/disabled instead of one on the server (which will replicate to ALL clients). You will have to move the code to a local script but you can just fire a RemoteEvent to the server whenever the player triggers the prompts so you can add and remove them from the entered array.

1 Like

How would I make the parts on the client? Would I get them from making new copies from server storage?

Actually after thinking about it further - you don’t even need to clone them. You can just move the Triggered events to a local script. This will ensure that they only trigger whenever the client triggers them, nullifying the player value it returns entirely. This will also allow you to enable / disable them without having to worry about it being replicated to other clients. Again, you will still have to fire a RemoteEvent in order to add them to the array on the server but that’s about it. You can do so like this:

-- CLIENT
local RemoteEvent = YourPathToRemoteEvent -- Reference the remote event
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)
-- SERVER
local entered = {}
local gui = game.Workspace.Exit.gui.BillboardGui.Frame
local RemoteEvent = YourPathToRemoteEvent -- Reference the remote event

RemoteEvent.OnServerEvent:Connect(function(player, request)
	if request == "Enter" then
		-- Insert player into array
		player.Character.HumanoidRootPart.CFrame = game.Workspace.Exit.gui.CFrame
		table.insert(entered, player)
	else
		-- Remove player from array
		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
		gui.players.Text = "Players: "..#entered
	end
end)

Just wondering where the local script would be.

For your case, it’s best to put the local script in StarterPlayerScripts. This is located under the StarterPlayer folder in the Explorer tab:

image

It worked, thank you for helping me.

1 Like

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