Scripting Problem

Hi Guys, I’m making a game as practice for my scripting as I’m finally committing to learning scripting. This might be easy for most people but here is my problem. I’m making my UI go from “Waiting for enough players” to “Intermission” and it’s not working, here is the code.

Status.Value = "Waiting For Enough Players"
repeat wait() until game.Players.NumPlayers >=2
Status.Value = "Intermission"

I made a string value called Status in ReplicatedStorage so I don’t really know why it isn’t working. Help would be appreciated!

2 Likes

Please send both scripts which are responsive to gui

2 Likes

if “NumPlayers” Represents a value under “Players” then you need to define the value instead of the instance itself. Try this:

repeat task.wait() until game.Players.NumPlayers.Value >= 2

Also, I do want to remind you to use task.wait() instead of wait() since its deprecated

2 Likes

Seems to be not working, I currently only have 1 script in the game which is my main script. Do I need to have a second script in the UI Part? I can send a screenshot if you are confused, sorry I’m very new to scripting

1 Like

No worries!

Can you explain what NumPlayers represents? I think that will help clear a few things out. What’s important is that whatever number represents the player count is actually changing, otherwise you will be stuck in a loop indefinitely. An easier way to check the amount of players is to use the # operator on tables. One example of a table is calling :GetChildren() or :GetDescendants() on an instance.

Try this instead:

repeat task.wait() until #game.Players:GetChildren() >= 2

Alternatively for the players folder you could also do this

repeat task.wait() until #game.Players:GetPlayers() >= 2
2 Likes

Inside of your ui you would need a local script that does this:

local Replicated = game:GetService("ReplicatedStorage")
local Status = Replicated.Status

Status.Changed:Connect(function()
--Change your Text to the Status.Value
end)
2 Likes

My Knowledge of NumPlayers is a bit limited but in other games I made I used this code and it worked most of the time. My game is sort of round based and I been told to put NumPlayers in that particular code, I’ll try the code you sent me. This scripting thing is harder then I thought lol.

2 Likes

I don’t quite get what you mean by change your text to status.value. Do I say that after Status.Changed:Connect(function()? Sorry, Just wanted to clear that up for me lol.

2 Likes

Yeah haha, scripting is tough, especially at first since it gets overwhelming super easily. I think a super important part about learning how to script isn’t only just the scripting but also how every piece of Instance works (so things you could basically create with Instance.new()) and what their properties do or mean.

In this example I would assume the scripts you have used in the past used either a “NumberValue” Instance or a “IntValue” Instance to represent a number, which in this case would represent the number of players within your game.
To get the number that is represented by these values you would need to first define where that value is stored (which you did game.Players.NumPlayers) and then add a .Value check after defining the instance to access the property which is called “Value”. This will return the number that the value is representing.
But its important that this number is also changing, if its not changing then the loop will just keep waiting. This is based on another assumption but I would assume that this value gets counted up or down depending if a player has entered or left your game therefor changing the actual value. This would allow the loop to break free and the code to continue.

But yeah I recommend you to just use my method since it does not require a third party function to control the value of NumPlayers and it instead just checks the number of player instances under Players.

(I do not recommend using :GetChildren() or :GetDescendants() in this case because these will catch all instances under Players, using :GetPlayers() insures that you are only creating a table for Player instances instead of everything)

2 Likes

Is this Correct?

repeat task.wait() until #game.Players.NumPlayers.Value >=2

1 Like

No. the # operator only works to check the “Number Of” in a table. I do not know what NumPlayers represents but I’m pretty sure its not a table. I would recommend just using this line:

repeat task.wait() until #game.Players:GetPlayers() >= 2
1 Like

Sorry for the late response.

You told that you want to change your UI Text to the Status Value right?

You will need a server script that changes the value and a client script that sets the Text to the value.

So basiclly in your Server Script you will need to check how much players are in the server and change the Status.Value - Then to change the ui you will need a local script somewhere in StarterGui or Playerscripts.

ServerScript would look like this:

local Players = game:GetService("Players")
local Replicated = game:GetService("ReplicatedStorage")
local Status = Replicated.Status

Status.Value = "Waiting for Players"
repeat wait() until #Players:GetPlayers() >= 2
Status.Value = "Intermission"

Then there are many ways to check if the Status Value has changed.
The simplest way to use is using .Changed

.Changed fires every time property in any object is changed

So your local script should look like this:

local Replicated = game:GetService("ReplicatedStorage")
local Status = Replicated.Status
local StatusLabel = ... --Put your TextLabel here

Status.Changed:Connect(function()
StatusLabel.Text = Status.Value
end)
1 Like

try

local playerValue
repeat 
    task.wait() 
    playerValue = game.Players.NumPlayers.Value
until playerValue >= 2

Didn’t work, I put this block of code in my main script

while true do

local Players = game:GetService("Players")
local Replicated = game:GetService("ReplicatedStorage")
local Status = Replicated.Status

Status.Value = "Waiting For Enough Players"
repeat wait() until #Players.GetPlayers() >= 2
Status.Value = "Intermission"

And In my local Script I put this

local Status = game:GetService(“ReplicatedStorage”):WaitForChild(“Status”)

script.Parent.Text = Status.Value

Status:GetPropertyChangedSignal(“Value”):Connect(function()

script.Parent.Text = Status.Value

end)

I have no idea why it isin’t working but I’ll still try and fix it, thanks for you help so far!

1 Like

you need to use a colon : and not a period . when calling :GetPlayers()

This line of code should look like this

repeat task.wait() until #Players:GetPlayers() >= 2

Yeah Sorry, I did a colon in my script accidently put a . in the post. Still not working, it’s so confusing because in other games I’ve made I’ve used this code and it has worked so I don’t know why it isn’t working. Do I have to reference the GUI in the code aswell? I doubt it though. I have a string value inserted in Replicated Storage so is it the wrong value? I doubt it also. Here is all of the code in the Main Script, is there any problems that could affect this?

– Services
local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local ServerStorage = game:GetService(“ServerStorage”)

local Status = ReplicatedStorage:WaitForChild(“Status”)
local MapsFolder = ServerStorage:WaitForChild(“Maps”)

– Game Loop

while true do

local Players = game:GetService("Players")
local Replicated = game:GetService("ReplicatedStorage")
local Status = Replicated.Status

Status.Value = "Waiting For Enough Players"
repeat task.wait() until #Players:GetPlayers() >= 2
Status.Value = "Intermission"

end

1 Like

This chunk probably doesn’t need to be in the loop since you only need to reference these once :stuck_out_tongue:

Anyway… where are you storing this script? And is this a LocalScript or ServerScript? If it’s a server script make sure its in ServerScriptService, if its a LocalScript put it in StarterPlayerScripts under StarterPlayer. I have a sneaky suspicion that this code isn’t even running at all. To make sure its running try debugging your code by putting a few prints where you think might be erroring to see if the script ever passes that point.

1 Like

So do I remove that chunk of code? Also I do have that script in my ServerScriptService (It is used as a serverscript so yeah) I also have a local script inside of the TextLabel which is inside of the Status GUI. So could that have been the problem?

no you don’t need to remove the code it just doesn’t necessarily be inside of the loop.

No this shouldn’t really cause issues (although I generally don’t like to store code inside of UI elements since it can get a bit messy and difficult to find over time). LocalScripts will run in StarterGui just fine, just keep in mind that scripts stored inside StarterGui will reset every time the player respawns (since StarterGui refreshes on respawn).

I copied your chunk of code and it was working as intended. Not exactly sure what the issue is. Are you sure you aren’t getting any errors? Maybe there are other parts of your scripts that are having issues and it might not even be with this piece of code. Again, try debugging your code :slight_smile:

1 Like

Interesting that it worked for you. I’ll try and debug the code now and tell you what happens. I really appreciate your help with this code, it was extremely helpful especially for a new scripter :slight_smile: