Player selector only selecting local player?

Now, I am making a player selector for a project I’m working on. And It selects a player, but when I tested it out on a local server in studio, it would always select the Local Player.

This is the same local server in the same round:


My code (I know its probably really bad, this is the first time iv’e done this):

-- Variables
local players = game.Players:GetChildren()
local ChaserTime = script.Parent.ChaserAndRoundTime.Value

-- New chaser
while true do
	local ChosenChaser = players[math.random(1,#players)]
	script.Parent.ChaserName.Text = (ChosenChaser.Name.." is the chaser!")
	print(ChosenChaser.Name.." is the new chaser")
	wait(ChaserTime)
end

Is this a Server Script? Try placing the script inside ServerScriptService.

Because you have a variable ‘Players’, it would only get the players once. You want it to get it each time you want a random player.

-- Variables
local ChaserTime = script.Parent.ChaserAndRoundTime.Value

-- New chaser
while true do
	local ChosenChaser = players[math.random(1,#game.Players:GetChildren())]
	script.Parent.ChaserName.Text = (ChosenChaser.Name.." is the chaser!")
	print(ChosenChaser.Name.." is the new chaser")
	wait(ChaserTime)
end
2 Likes

Okay, I put it in Server Script Service and got this error. I don’t understand why I have it

Screen Shot 2021-02-07 at 9.39.52 AM

Remove the lower bound.

math.random(#game.Players:GetChildren())

as opposed to

math.random(1,#game.Players:GetChildren())

Also, you can continue on the loop if there are no children in game.Players

GetPlayers is the canonical function for fetching the list of players. It’d be best to make it a habit to use this over GetChildren, just so your code has more clarity to it.

In other news, your code doesn’t seem to be incorrect other than the fact that you’re only rolling indices from the first time you get the table of players. Updating your player table in the loop would resolve your issue, which someone already suggested.

In rabbi’s solution, the interval becomes nil because it’s trying to index an entry from a table that no longer exists as a variable. A quick cleanup can resolve that problem.

local Players = game:GetService("Players")
local ChaserTime = script.Parent.ChaserAndRoundTime.Value

while true do
    local serverPlayers = Players:GetPlayers()
    local chosenChaser = serverPlayers[math.random(#serverPlayers)]
    script.Parent.ChaserName.Text = chosenChaser.Name .. " is the chaser!"
    print(chosenChaser.Name, "is the new chaser")
    wait(ChaserTime)
end
1 Like

It works for the first round, but after that it breaks…

Can’t help you if you don’t provide any information here. If it’s still not working, please provide more context than “it stops working”. For example, if there are any error messages or if you’ve made modifications to the code. If you do not provide details, I cannot help you further.

First round:

Second round:

No errors:
Screen Shot 2021-02-07 at 10.08.21 AM

How exactly is this broken then? There’s no errors and the code is running as expected. Your random generator simply picked Player1 three times in a row as the “chaser”. There’s no implementation to prevent the same player from being chosen twice. It’s not broken. If you don’t want the same player to be chosen twice, you need to code that behaviour yourself.

No, look closely. Look at the second screen shot, there cannot be two chasers at a time.

This is why I’m saying you need to provide details. I can’t tell at a glance what your problems are if you don’t explain them in detail and then it leads me to need to ask lots of questions to figure out the problem; you need to provide this information yourself.

And in that, I still can’t figure out what your problem is because there’s no detailed description of your circumstances. What do you mean by what you say? It seems like you have a completely unrelated problem here. The round script provided resolves the issue of your player selector only selecting between one player as the target. The rest stems from your other code and is a separate problem that you should look into.