Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

How can i Randoomly Choose a Player in a server and Show there username using TextLabel? [closed]

Asked by 4 years ago
Edited 4 years ago

I Want to script my Text Label in my game with a Label That randomly chooses a roblox a player in my server and get there username For a Example i want it to be like this:

Player1 : I don't Feel good!

Player5: I Found a Med Kit!!!

Like That if you know how to make a text label like that it would really Help! It would be more Helpful if you post code!

0
This site is a help site, NOT a request site. youtubemasterWOW 2741 — 4y

Closed as Not Constructive by Ziffixture

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

2 answers

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

If you want to randomly choose a game object, you would use math.random()

How To Use Math.Random()

math.random() takes 2 arguments, the minimum value that it can go, and the maximum value it can go. So, it would look like this, math.random(1,10), this would choose a random number between 1 and 10. We still have to set it to a variable to use it.

local randomNum = math.random(1,10)
print(randomNum) --// Can print a number from 1 to 10.

Result: killerbrenden - Since I would technically be the only one in the server.


If you want to randomize a list of objects, you should get the number of children the parent contains, and randomize that number and index the parent to get the random position in the list!

So, if you wanted to do it with players, you would do something like this.

local players = game:GetService("Players"):GetPlayers()
local randomPlayer = players[math.random(1,#players)]
print(randomPlayer.Name) --// Would print the random player's name

Result: killerbrenden - Since I would technically be the only one in the server.

Explanation

So, what we're doing here is getting the list of all the players in a table (Line 1). Next, we're indexing the players table, that contains all the players, and we're randomizing the number we're indexing by with math.random(1,#players), this will choose a number between 1 and the number of players there are in the game. Then, it will use that number, and index the players table with it, this will then choose a random player from the list and that is your player object. So, you can do randomPlayer.Name, randomPlayer.Character, etc.


Source


Hope this helped explain math.random!

Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago
local Players = game:GetService('Players'); -- The Players service
local ListPlayers = Players:GetPlayers(); -- An array of all the Players in the game
local NumPlayers = #ListPlayers; -- The number of Players in the game

local RandomPlayer = ListPlayers[math.random(1, NumPlayers)]; -- A random Player from the list

math.random - Takes a random number between the two arguments (API Link)

When you create an array like I've done above, each value will have an index (position in the array). You can index by using ArrayName[number], and I have substituted our random number in so it is indexing a random entry in our array of Players.