Random Character Selection Script

Effectively, what I’m trying to achieve here is a script that will decide on two unique players in the workspace and teleport them to two opposing corners of a boxing ring.

However, I’ve been told that my way of going about this is quite inefficient and I’m not really sure how: I’ve tried a few ways of performing this but I can’t seem to rack my brain around it

The script goes as follows:

local Players = game.Players:GetChildren()() 
local Chosen1, Chosen2 = math.random(1, #Players), 0
repeat Chosen2 = math.random(1, #Players) until Chosen2 ~= Chosen1
Chosen1.Character.HumanoidRootPart.CFrame = Corner1.CFrame
Chosen2.Character.HumanoidRootPart.CFrame = Corner2.CFrame

I’m not necessarily asking for anyone to write a full script for me, just some advice in optimising this script: any help would be appreciated; thanks!

1 Like

local Players = game.Players:GetPlayers()
local Chosen1, Chosen2 = math.random(1,#Players),0
repeat Chosen2 = math.random(1, #Players) until Chosen2 ~= Chosen1
Players[Chosen1].Character.HumanoidRootPart.CFrame = Corner1.CFrame
Players[Chosen2].Character.HumanoidRootPart.CFrame = Corner2.CFrame

I don’t see how there could be a more “efficient” way of doing this, but there were definitely a few errors.

Sorry, would you mind telling me the errors?

local Players = game.Players:GetChildren()() --Extra ()

Chosen1.Character.HumanoidRootPart.CFrame = Corner1.CFrame
Chosen2.Character.HumanoidRootPart.CFrame = Corner2.CFrame --Character is not a child of a number

2 Likes

Oh, of course! Thanks a bunch.

local Players = game:GetService("Players")
local PlayerList = Players:GetPlayers()
local Chosen1, Chosen2 = math.random(1, #PlayerList), nil
repeat Chosen2 = math.random(1, #PlayerList) until Chosen2 ~= Chosen1
local Player1 = PlayerList[Chosen1]
local Character1 = Player1.Character
local HRP1 = Character1:WaitForChild("HumanoidRootPart")
local Player2 = PlayerList[Chosen2]
local Character2 = Player2.Character
local HRP2 = Character2:WaitForChild("HumanoidRootPart")
HRP1.CFrame = Corner1.CFrame
HRP2.CFrame = Corner2.CFrame
1 Like