I have an NPC, and in its head I have a dialog with a dialog choice and a script in it. I want to make the response dialog from the NPC be a random's player name from a certain team. I don't know how to make it from a certain team, but I've tried to make the first part of the script, tho, it didn't worked...
e = game.Players.random.Name script.Parent.DialogChoice.ResponseDialog = "Your target is" + e + "."
I have a feeling I'm not even close to a good way of doing this so, any answers would help.
If you use e = game.Players.random.Name
, you are trying to index a child (Player) named random
. If you are trying to choose a random player, start by getting all the players (game.Players:GetPlayers()
) which returns a table of ALL the players. Then, use math.random()
to choose the target (Player) randomly.
Let me show you.
local players = game:GetService("Players"):GetPlayers() -- GetService works too! This gets all of the players local RandomPlayer = players[math.random(1, #players)] -- #players = The number of players. This chooses the player by the numeric indices from the table returned by :GetPlayers() script.Parent.DialogChoice.ResponseDialog = "Your target is" + RandomPlayer.Name + "." -- The RandomPlayer returns only the Player object, you need to Index the name property of the player in order for this to work.
Any questions? Leave them in the comments. Thanks!