I’m making a hitman quest thingy, and I want to make it so it picks a random player, but how can I make it so it doesn’t pick yourself?
--what i currently have
local player = game.Players:GetChildren()
local player = player[math.random(1, #player)]
script.Parent.Parent.PersonToKill.Value = player.Name
also, i’m making it so it ignores anyone under a certain level, how do I make it so it rerolls until it finds a player who isnt you and has a level above that level requirement?
local LocalPlayer = game. Players.LocalPlayer
local players = game.Players:GetChildren()
local RandomPlaye r= player [math.random(1, #player)]
repeat RandomPlayer = player [math.random(1, #player)] until RandomPlayer ~= LocalPlayer
script.Parent.Parent.PersonToKill.Value = RandomPlayer .Name
I do however highly recommend organizing your code.
--//Services
local Players = game:GetService("Players")
--//Variables
local Player = Players.LocalPlayer
--//Functions
local function ChooseRandomPlayer()
local RandomPlayer= player[math.random(1, #player)]
repeat RandomPlayer = player [math.random(1, #player)] until RandomPlayer ~= LocalPlayer
end
--//Events
-- ?
--//Main
local RandomPlayer = ChooseRandomPlayer()
script.Parent.Parent.PersonToKill.Value = RandomPlayer.Name
-- Services
local Players = game:GetService("Players")
-- Variables
local PlayerList = Players:GetPlayers()
local LocalPlayer = Players.LocalPlayer
-- Initialize
table.remove(PlayerList, table.find(PlayerList, LocalPlayer)) -- woops!
-- One-time use
script.Parent.Parent.PersonToKill.Value = PlayerList[math.random(#PlayerList)].Name
-- Multiple uses, call function when necessary
local function getNewTarget()
script.Parent.Parent.PersonToKill.Value = PlayerList[math.random(#PlayerList)].Name
end
Canonical use of Players:GetPlayers()
Note that math.random() with one argument automatically sets it as maximum value.
Edit: Fixed a missing directory of a couple lines, also lacks level checks, which can be done through a for loop through the existing table of players.
You don’t have to make it reroll if it gets someone under a certain level, you can just sort out the table so it doesn’t include you or anyone under that level.
local You = game.Players.LocalPlayer
local AllPlayers = game.Players:GetChildren()
for _, player in pairs(AllPlayers) do
if player == You or player.Level < 5 then
table.Remove(AllPlayers, table.Find(AllPlayers, player)
end
end
local Target = AllPlayers[math.Random(1, #AllPlayers)]
local plr = game.Players.IgnoreMe --replace this with the player you want to ignore
local list = {}
for _,v in pairs(game.Players:GetPlayers()) do
if v ~= plr then
table.insert(list,v)
end
end
local random = list[math.random(1,#list)]
The best way to do this is to create a table of players that meet your requirements, i.e.
Not the local player
Above a certain level
local PlayersService = game:GetService("Players")
local candidates = {}
for _, player in ipairs(PlayersService:GetPlayers()) do
if player ~= PlayersService.LocalPlayer then
if level(player) >= minimumLevel then -- however you check their level
table.insert(candidates, player)
end
end
end
Then, simply pick one player from that table. You can safely pick any player as the only players in the table are ones which meet your requirements. Be careful to make sure there is at least one player in the table first.
if #candidates == 0 then
-- no players meet your requirements
else
local index = math.random(#candidates)
local player = candidates[index]
-- player is your chosen player
end
This method is better than doing the “choose a random player until they meet your requirements” method suggested above, because that method may actually run forever if no players meet your requirements, or take a very long time if there are only a few eligible players out of a big number of players.
Some of the suggestions above also include making a list of players and then removing the players that do not meet your requirements. I think that the method I have suggested is preferable to that because it is more explicit about building a list of eligible players, and because removing from a table inside a loop is easy to get wrong.
local players = Game:GetService("Players")
local curr = players.LocalPlayer
local rand = tostring( players[math.random(1, #players)].Name )--get a random player's name
if not rand == tostring(curr.Name) --if the random player is not you, print their name
then
return
print(tostring(rand.Name))
end
excuse me if anything i’m doing is deprecated, haven’t coded in (literal) years.