Script Only Gives Value to One Player

I don’t really know what the Title should be since I can’t really describe it much. Anyway Im creating a script which Selects different people to have different roles for Say. Somebody may be a Medic someone else may be a Swordsman or Guard.

local Select = Instance.new("StringValue")
	local Z = Select:Clone()

				repeat  RandomPlayer = players [math.random(1, #players)] until RandomPlayer:FindFirstChild("Value") == nil
				
				Z.Value = Table [math.random(1, #Table)]

The script should take every player and give it a role. However instead of doing it to everyone it does it to only one person. I have a good idea why it does this I can’t think of a solution.

1 Like

So, can you elaborate a bit more on this? How many players are allowed in each server and how many roles are there to be selected in the game?

Can you also show a video or the output of what’s giving you an error?

Theirs about 6 Roles per Server leaving 6 Players per Server. Each Role is Different. No errors are shown in the Output

I don’t get even the slightest bit what you are doing.

If that is so then

Is fine because it selects one person out of the whole game to give it to

By that I mean it takes everyone and gives them random roles. Sorry for the inconvenience

You could loop through the players and check if they have a team and if they don’t then choose a random one that has not been taken then assign it to the player (not the best way)

Doesn’t work. Instead it gives me an error

What;s the error? We need this information

I’m not very sure of the problem your having but if your looking to give a random role to a player that joins I think this should do it :+1:

local Players = game:GetService("Players") -- Players Service

local Roles = {"Role1","Role2"} -- The roles that are available for the players

Players.PlayerAdded:Connect(function(plr) -- Function Connects when Player joins
    local Select = Instance.new("StringValue",plr) -- Create the Role String Value
    Select.Name = "Role" -- Give it a Name
    Select.Value = Roles[math.random(1,#Roles)] -- Give the Role to the player
end)
1 Like

Check if the “Value” value is under the player

You are setting a random player’s value to something until that same player’s value is something.
My suggestion is, use for loops.

for _,Player in pairs(game:GetService("Players"):GetPlayers()) do
  local Select = Instance.new("StringValue")
  Select.Parent = Player
  Select.Value = Table[math.random(1,#Table)]
end

You can add a role to a player on joined using this reply.

Hope this works for you!