Why would this script not work?

print(“Hello world!”)

So, I’m a fairly new scripter, and I have no idea what I’ve done incorrect, since it functions properly in studio, but not in actual servers!

This code is inside my ServerScriptService ( And yes, the remote event is in replicated storage)

   --//Variables 
local AMTNEEDED = 5
local Timer = 10


local ReplicatedStorage = game:GetService("ReplicatedStorage")

local TimeFunction = game.ReplicatedStorage:WaitForChild("Timer")

--//Functions

	
	
	
--// Main Script

while wait() do
	local Players = game:GetService("Players"):GetChildren()
	
	if #Players >= AMTNEEDED then
		print ("hi")
	else
		for i,v in pairs(Players) do 
			local p = game.Players:FindFirstChild(v)			
		ReplicatedStorage.NotEnoughPlayers:FireClient(p,AMTNEEDED-#Players)
		print("Dope")
		end
		
	end
end

Basically this is the start of my minigame type script, following this, in a text label that is inside a gui, I have this script -

--// Functions
function NotEnough (Amount)
	script.Parent.Text = "You need ".. Amount.." More to play!"
end
game.ReplicatedStorage.NotEnoughPlayers.OnClientEvent:Connect(NotEnough)

This is suppoes to keep checking if there is enough players, else it will relay said text… but this only works in studio, what did I do incorrectly?

Thank you
~Revelted

1 Like

Some things I’m assuming:

  • experimental mode is false
  • function NotEnough is in a local script
  • function NotEnough is triggered as a result of NotEnoughPlayers.OnClientEvent()

Also, some things worth pointing out would be that NotEnoughPlayers is only fired when there aren’t enough players (meaning that when there are enough it won’t change from “You need x more to play!” to “The game is beginning.”

Confirm these for me and I’ll see if I can help anymore

For the first point, My filtering is enabled, second point, I’ve tried both, but I keep it inside a local script and point three, I’snt that the point? when there isnt enough players, it fires to all the players of the server.

as for the note, the round system is set on a “while wait()” loop, so when there is enough people, I’m going to have a separate event firing which starts the round and changes the text :stuck_out_tongue:

Update:

I found this error, yet im pretty sure i label the players name.

erro

The value of v is the player object - since you are getting the children of Players, it makes sense that the children would be player objects.

FindFirstChild takes a string, not an object, as an argument, so it returns nil (which isn’t a player object).

v is already the player object, so there is no need to make a player variable.

2 Likes

FacePalm oml im so dumb :joy: Thanks man, that works fine lmao

2 Likes

Emerald beat me to it after looking at the code for a few minutes.

Here’s a quick rundown on for loops in this scenario:

for _,v in pairs(game.Workspace:GetChildren()) do
print(v)
end 

→ Terrain
→ Camera
etc…

Also, an off-topic suggestion, it may be a good idea to have the clients monitor a replicated ValueBase instead of bombarding your remotes with requests.

What do you mean? sorry, like i said above, i am still new to all this >.<

As your problems been solved (as seen above), I think it might also be beneficial to use .PlayerAdded and .PlayerRemoving for the player count on the client and again on the server for the server related functions. Not sure if that’s what @SummerEquinox was suggesting but if it was it would reduce how often your events are fired and then you wouldn’t have that while wait() loop.

Edit: nope, he’s suggesting using values. Follow his suggestion as it’s more efficient.

No problem, have a replicated ValueBase (as they will soon be called) in workspace, for example. Then, since it’s replicated automatically, you can monitor it from the client instead of on the server, removing the need for RemoteEvent usage here.

There’s nothing wrong with his current setup, generally I do this:

while game.Players:GetChildren() < desiredNumber do

He was just firing events in unneeded places. Nonetheless, the thread has been solved so we should probably wrap this up.

1 Like

Oh! You mean you just want me to put an IntValue inside workspace, and have functions playerAdded/Removing to fluctuate the int value?