Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I make it so when 2 players join my game, the round starts?

Here's my attempt, please tell me what is wrong. I am trying to make a roblox version of Escape The Night, but for it to begin in the pre-alpha stages, at least 2 players need to be in the game

game.local Players = game:GetService("Players")
 local P = game.workspace.Play

Players.PlayerAdded:Connect(function(player)
 	print(player.Name .. " joined the game!")
	P.Value = P.Value + 1
end)
  Players.PlayerRemoving:Connect(function(player)
	print(player.Name .. " left the game!")
	P.Value = P.Value - 1 end)
 local v = game.Workspace.Play.Value v:GetPropertyChangedSignal("Value"):Connect(function()
 if P.Value >= 1 then
 	game.Workspace.Beep:Play()
 	game.StarterGui.ScreenGui.TextLabel.Text = "The party is about to start..."
	wait(2)
	game.Workspace.Beep:Play()
	game.StarterGui.ScreenGui.TextLabel.Text = "Don't scream too loud, you'll wake him up."
 	wait(2)
	game.Workspace.Beep:Play()
	game.StarterGui.ScreenGui.TextLabel.Text = "But most importantly, have FUN!"
	print("Success")
end	 
end)

4 comments

0

How is it running? Is it working but no the way you want it? Or not working at all?

0

it looks like line 12 he made it >= to 1 player. btw i figured out the number problem for the spectate

0

You should remove the game.local part in the first line and just do local Players = game:GetService("Players")! I hope this works! PS. In line 12 remove the "=" and just keep the ">"

0

You're making it too complex..

3 answers

✓ Accepted answer

The Issue? You're not scripting in FE.

What do I mean by FE? FE stands for Filtering Enabled, the type of client-server system Roblox has used on all games since 2018. It's a game industry standard, meant to help minimize exploits. Essentially, the system is like this:

Any change on the client stays on the client, it doesn't change on the server. Any change on the server, though, will make the change on all clients, too. The server cannot touch anything ONLY on the client, however.

As a result, any scripting revolving the client cannot be scripted from a regular script. Assuming this is in ServerScriptService or Workspace (I personally prefer the former), it's not working because you're trying to change the UI on the server. UI is part of the client, so the server doesn't have "access" to it. Therefore, using StarterGui won't do anything on the server. Not to mention that StarterGui wouldn't be how you change the GUI. In order to change UI, you need to use a RemoteEvent. This fires an event down to the client, which can then receive it and change the UI if needed.

This is what'd happen when scripting while respecting FE:

--SERVER SCRIPT
--Create a remote event and place it in replicatedstorage
game.local Players = game:GetService("Players")
 local P = game.Workspace.Play --Capitalization is key!

Players.PlayerAdded:Connect(function(player)
    print(player.Name .. " joined the game!")
    P.Value = P.Value + 1
end)
  Players.PlayerRemoving:Connect(function(player)
    print(player.Name .. " left the game!")
    P.Value = P.Value - 1
 end)
 P:GetPropertyChangedSignal("Value"):Connect(function() --No need to change to V, as it's the same thing!
 if P.Value > 1 then --It'll run at 2 like that
    game.ReplicatedStorage:FireAllClients() -- Fires a message to all clients
    print("Success")
end  
end)

You'd need a local script, too now! This is assuming if it's in starter gui:

local PlayerGui = game.Players.LocalPlayer:WaitForChild("PlayerGui") -- This is the actual UI you want to change, Starter GUI replicates itself into the player
game.ReplicatedStorage.RemoteEvent.OnClientEvent:Connect(function() --How to detect event on client
game.Workspace.Beep:Play()
    PlayerGui.ScreenGui.TextLabel.Text = "The party is about to start..."
    wait(2)
    game.Workspace.Beep:Play()
   PlayerGui.ScreenGui.TextLabel.Text = "Don't scream too loud, you'll wake him up."
    wait(2)
    game.Workspace.Beep:Play()
    PlayerGui.ScreenGui.TextLabel.Text = "But most importantly, have FUN!"
end)

If you need more reference: https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events https://developer.roblox.com/en-us/api-reference/property/Workspace/FilteringEnabled

In line 12 you made it so if there is greater or "equal" to 1 player. Take out the "=" if that's not it please message me back.

Here's a script i'm using for my game:

local playernumber

game.Players.PlayerAdded:Connect(function(p)
	if playernumber == nil then
		playernumber = 1
	else
		playernumber = playernumber +1
	end
end)

game.Players.PlayerRemoving:Connect(function(p)
	playernumber = playernumber -1
end)

function playercount()
	for number,plrs in pairs(game.Players:GetPlayers()) do
		if number == nil or plrs == nil then
		return 0
		else
		return number
		end
	end
end

while wait(1 + math.random()) do
	if playercount() >=2 then
		print"ok game starting"
		break
	else
		print"you need 2 players to start"
	end
end

if it works accept , thank you.

This discussion is preserved as it was. New answers, comments, and votes are closed.