Putting a random player's name in a TextLabel

I was trying to make a localscript inside a textlabel that chooses a random player, takes its name, and puts it in the text. I tried making a Player.name.random thing, but it doesn’t work. Help plz!

Stay safe
ParkCityUSA

2 Likes

I’d recommend posting some of your code, Also, wrong topic, should be in scripting support

1 Like

This is my script. It’s in the red circle:

It’s super tiny so sorry

Cool Creations is not for getting help on coding problems, it is for showcasing things that you’ve created. Please read category guidelines before posting threads. I’ve moved this to Scripting Support where it belongs, but you should also read the category guidelines here and supply the code for the attempt you made in creating this system as well as any other details that’d be useful to understand the problem you’re encountering. I’ve also made the title more descriptive of your problem.

2 Likes

You have to pick a random player with math.random and get his name.
Just do something like

local Players = game:GetService("Players")

local function pickRandomPlayerName()
    return Players:GetPlayers()[math.random(1, #Players:GetChildren())]
end

Also, if you want a typing effect, I’d recommend doing something like this

local firstText = "Hello scientists! Welcome to this Plasmat Labs!"
local thirdText = "Ummm, complications!"

local function pickRandomPlayerName()
   return Players:GetPlayers()[math.random(1, #Players:GetChildren())]
end

local randomPlayer = pickRandomPlayerName()
local fourthText = randomPlayer .. " let's set up!"

for i = 1, #firstText do 
     script.Parent.Text = string.sub(firstText, 1, i)
     wait()
end
--Now it ended
for i = 1, #secondText do 
   script.Parent.Text = string.sub(secondText, 1, i)
   wait()
end
--Now it ended
for i = 1, #thirdText do 
   script.Parent.Text = string.sub(thirdText, 1, i)
   wait()
end

instead I suggest doing the following 2 things:

-- type writer function
local function typeWriter (textString, delayTime)
    for i = 1,#textString,1 do
        textLabel.Text = string.sub(text,1,i)
        wait(waitTime)
    end
end

typeWriter("Hello, scientists. Welcome to Plasmat Labs recently abandonded due to...",.1)
typeWriter("Um, complications",.1)

the second thing the randomsizer:

-- random player picker
local Players = game:GetService("Players"):GetPlayers()
local nPlrs = #Players

local RandomPlayer= nil
if nPlrs > 0 then
     RandomPlayer = Players[math.random(1, nPlrs)]
end

typeWriter(RandomPlayer.Name..": Yes, it's been set up.",.1)
2 Likes

Ok! I’m going to try it, and thanks for the typewriter tip! I was wondering if there was a faster way!

1 Like

No worries, I just noticed a few flaws in the script and fixed them, use the edited script.
Oh and don’t forget to close the topic after it works! Thanks! :smiley:

2 Likes