How can I create a pick a random person to chat function?

Info: I am working on a revamp of a classic game known as Kool Killer. I wanted to make a function that randomly selects a player and then when a line of code is run a bubble appears above their head saying the dialogue of my choosing. I already have functions that work for the bubble chat, but I want one that chooses a random player.

This is the regular function I want to modify to randomly select a player to chat:

function Chat(player,text,color)
	local ChatService = game:GetService("Chat")
	if game.Players:findFirstChild(player) then
		if game.Players[player].Character:findFirstChild("Torso") then
			ChatService:Chat(game.Players[player].Character.Head,text,Enum.ChatColor[color])
		end
	end
end

h.Value = "Person: When are we stopping at Burger King?"
Chat("Person","When are we stopping at Burger King?","Blue") --Example of the regular function being ran

You could include these 2 lines

local players = game:GetService("Players"):GetPlayers()
local randomPlayer = players[math.random(#players)]

To get the players ingame and then to select a random player from the players in game and then change the code to use randomPlayer

Slight example would be this

function Chat(text,color)
	local ChatService = game:GetService("Chat")
	local players = game:GetService("Players"):GetPlayers()
	local randomPlayer = players[math.random(#players)]
	if randomPlayer.Character and randomPlayer.Character:FindFirstChild("Torso") then
		ChatService:Chat(randomPlayer.Character.Head,text,Enum.ChatColor[color])
	end
end

h.Value = "Person: When are we stopping at Burger King?"
Chat("When are we stopping at Burger King?","Blue")

You may need to also include the h.Value in there if you want it to include the name of who is chatting

1 Like

image I get this error

Is the script running this immediately? It’s erroring because by the time the script runs, there’s no players in game, try adding a very delayed wait such as wait(15)?

I only suggested a wait(15) a temporary solution, it’s likely stickpuppet will be using the function when players are alreayd in game

Edit: Okay how did my temporary solution led to 4 more replies about the best way to ensure that at least a player is in game when I’m pretty sure @OP already has that accounted for in his main code

Bad solution, I suggest checking if #players > 0.

1 Like

I agree, a simple repeat loop would work.

repeat wait() until #game.Players:GetChildren() > 0

Even worse, it depends what OP is trying to achieve, but first of all, the check should be inside the function and if #players == 0 it should skip the rest the code in the function. We need to prevent the error from happening in all cases, not fit our code and hope that the error case won’t happen.

And instead of all these suggested waits, if you really want your code to wait for the player count to be > 0, you can do:

local players = game:GetService("Players")
if #players:GetChildren() == 0 then
    players.PlayerAdded:Wait()
end

How could I include the name of the person in the h value? Sorry if I am bugging you I don’t know how I could write it

Hi sorry for my late response I was asleep! And it’s fine! Basically since randomPlayer contains a player instance, you just do randomPlayer.Name to get the name

With some slight concatenation it would be

h.Value = randomPlayer.Name .. ": When are we stopping at Burger King?"

I tried this with 2 players ingame before, but it makes one player say something with the bubble and the other one on the h value. Basically, it has different players saying the same thing when there should only be one player saying these things.

Could it be how you set up the h.value and the chat? May I see the current code you have?

Here is the code I am using right now in my test game to test this

local h = game.Workspace.InfoReel

wait(5)
local players = game:GetService("Players"):GetPlayers()
local randomPlayer = players[math.random(#players)]

function Chat(text,color)
	local ChatService = game:GetService("Chat")
	local players = game:GetService("Players"):GetPlayers()
	local randomPlayer = players[math.random(#players)]
	if randomPlayer.Character and randomPlayer.Character:FindFirstChild("Torso") then
		ChatService:Chat(randomPlayer.Character.Head,text,Enum.ChatColor[color])
	end
end
while true do
	h.Value = randomPlayer.Name..": When are we stopping at Burger King?"
	Chat("When are we stopping at Burger King?","Blue")
	wait(3)
	h.Value = randomPlayer.Name..": SHUT UP"
	Chat("SHUT UP","Blue")
	wait(5)
end

Put the h.value lines in the Function as well

local h = game.Workspace.InfoReel

wait(5)

function Chat(text,color)
	local ChatService = game:GetService("Chat")
	local players = game:GetService("Players"):GetPlayers()
	local randomPlayer = players[math.random(#players)]
	if randomPlayer.Character and randomPlayer.Character:FindFirstChild("Torso") then
		ChatService:Chat(randomPlayer.Character.Head,text,Enum.ChatColor[color])
        h.Value = randomPlayer.Name .. text
	end
end
while true do
	Chat("When are we stopping at Burger King?","Blue")
	wait(3)
	Chat("SHUT UP","Blue")
	wait(5)
end
1 Like

Thank you so much for your help, I appreciate it.

1 Like