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?
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
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.
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.