How to get 1 player out of the 4 players joining. HELP NEEDED!

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

How to check for 1 singular player out of the 4 joining and fire a event to the server.

  1. What is the issue? Include screenshots / videos if possible!

I cannot really test the output but these are the issues I get for the client.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I tried to use # and other types of methods like GetChildren()

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TeleportService = game:GetService("TeleportService")

local mapData = TeleportService:GetTeleportSetting("MapData")
local event = ReplicatedStorage:WaitForChild("getMap")


Players.ChildAdded:Connect(function(player)
	if player then
		if #Players:GetPlayers() == 1 then
			event:FireServer(mapData)
			task.wait(0.25)
			script:Destroy()
		end
	end
end)

YES IT DOES WORK, ALL I NEED TO DO IS MAKE IT WORK FOR ONE SINGULAR PLAYER OUT OF THE 4 SO ALL OF THE PLAYERS DON’T FIRE TO SERVER!


image

Just have the server use a bindable event to handle it.

local TeleportService = game:GetService("TeleportService")
local mapData = TeleportService:GetTeleportSetting("MapData")
local event = --bindable event
local done = false

game.Players.PlayerAdded:Connect(function(plr)
    if #game.Players:GetPlayers() == 1 and not done then
        event:Fire(mapData)
        done = true
    end
end)

Edit: Whoops, GetTeleportSetting is only available on the client. So, instead, use a bindable event and a remotefunction. This makes this much less exploitable.

local event = --bindable event
local remfunc = --remove function
local done = false

game.Players.PlayerAdded:Connect(function(plr)
    if #game.Players:GetPlayers() == 1 and not done then
        event:Fire(remfunc:InvokeClient(plr))
        done = true
    end
end)

client script

local TeleportService = game:GetService("TeleportService")
local mapData = TeleportService:GetTeleportSetting("MapData")

remevent.OnClientInvoke = function()
    return mapData
end

Oh I should of explained both scripts I had before! Hold on! When it is fired to Server it leads to this script:

local RS = game:GetService("ReplicatedStorage")
local TeleportService = game:GetService("TeleportService")
local ServerStorage = game:GetService("ServerStorage")

local event = RS:WaitForChild("getMap")

local maps = ServerStorage.Maps

event.OnServerEvent:Connect(function(player, mapData)
	if mapData then	
		maps:WaitForChild(mapData).Parent = workspace.Map
		task.wait(5)
		workspace.SpawnBox.Floor:Destroy()
	else
		maps["Grass Lands"].Parent = workspace.Map
		task.wait(5)
		workspace.SpawnBox.Floor:Destroy()
		warn("Default Map Loaded")
	end
end)

Well, that shows that you don’t actually need the player and it will work fine with a bindable event.

event.Event:Connect(function(mapData)

You could also do this without this much communication, just using the one script I had provided before (partially)

local remfunc = --remove function
local done = false

game.Players.PlayerAdded:Connect(function(plr)
	if #game.Players:GetPlayers() == 1 and not done then
		local mapData = remfunc:InvokeClient(plr)
		if mapData then	
			maps:WaitForChild(mapData).Parent = workspace.Map
			task.wait(5)
			workspace.SpawnBox.Floor:Destroy()
		else
			maps["Grass Lands"].Parent = workspace.Map
			task.wait(5)
			workspace.SpawnBox.Floor:Destroy()
			warn("Default Map Loaded")
		end
		done = true
	end
end)

Im a bit confused, so theres a new script? So the client script that I was on about first thing is in StarterPlayerScripts. So that will become the onClientInvoke one? I really need a summary of which scripts going where, i understand what it does but i am pretty overwhelmed right now.

hmm

if #Players:GetPlayers() <= 2 then

Alright, sorry for not explaining properly. The first script is a server script, the one that requests the data from the first player that joins, and only once. The second script will be a script inside StarterPlayerScripts like you had thought, which will return the map data.

I shall test it now then. Hopefully there will be not many issues.

maybe something like this?

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TeleportService = game:GetService("TeleportService")

local mapData = TeleportService:GetTeleportSetting("MapData")
local event = ReplicatedStorage:WaitForChild("getMap")

Players.PlayerAdded:Connect(function(player)
	if player then
		if Players:GetPlayers() == 1 then
			event:FireServer(mapData)
			task.wait(0.25)
			script:Destroy()
		end
	end
end)

your checking the instance, not the number??

client:

server:

ServerScript:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remfunc = ReplicatedStorage.mapFunction
local done = false

game.Players.PlayerAdded:Connect(function(plr)
	if #game.Players:GetPlayers() == 1 and not done then
		local mapData = remfunc:InvokeClient(plr)
		if mapData then	
			maps:WaitForChild(mapData).Parent = workspace.Map
			task.wait(5)
			workspace.SpawnBox.Floor:Destroy()
		else
			maps["Grass Lands"].Parent = workspace.Map
			task.wait(5)
			workspace.SpawnBox.Floor:Destroy()
			warn("Default Map Loaded")
		end
		done = true
	end
end)

Client Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TeleportService = game:GetService("TeleportService")

local mapData = TeleportService:GetTeleportSetting("MapData")
local event = ReplicatedStorage:WaitForChild("getMap")

event.OnClientInvoke = function()
	return mapData
end

image

Oh whoops, make sure to change getMap to a remote function. The bindable function isn’t needed anymore either.

So theres 2 different remote functions? Or is there only 1?

There is only one remote function.

local remfunc = ReplicatedStorage:WaitForChld("mapFunction")

Read my mind exactly how I thought, I am testing it now.

Got some new errors.

Server:

This is what brung me here in the first place, how it loads the default map and not the suggested map.

The ‘Main’ script does work however now. It’s fixing the first part is all.

are you sure thats a remoteevent or not

Is the client actually receiving any map data? You might want to make sure of that first.

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TeleportService = game:GetService("TeleportService")

local mapData = TeleportService:GetTeleportSetting("MapData")
local event = ReplicatedStorage:WaitForChild("getMap")

print(mapData)

event.OnClientInvoke = function()
	return mapData
end

This is what is shown: I am meant to be at Nuclear Bunker.

The game thinks its nil…

image

What else: Well this is the script for the lobby elevator, which I soon will refer back to once its fixed.

ELEVATOR SCRIPT:

local function TeleportPlayers()
	local placeId = config:WaitForChild("PlaceID").Value
	local server = TeleportService:ReserveServer(placeId)
	local options = Instance.new("TeleportOptions")
	options.ReservedServerAccessCode = server
	
	TeleportService:SetTeleportSetting("MapData", mapgui.MapName.Text)
	safeTeleport(placeId, playersWaiting, options)
end

I reccomend you mainly look at the SetTeleportSetting part…