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
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
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
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.
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.
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
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