Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why wont this work? (Functions ect)

--(iLegimate)--

function choseBH()
local bountyhunters = game.Players:GetPlayers()[math.random(2,#game.Players:GetPlayers())]
		bounthunters.Character:MoveTo(game.Workspace.TPTO1.Position or game.Workspace.TPTO2.Position)
	-- ADD WHAT BOUNTY HUNTERS GET --
	-- ADD HUNTER's SPAWN(HQ) --
end

function chosePOLICE()
	local police = game.Players:GetPlayers()[math.random(2,#game.Players:GetPlayers())]
	-- ADD WHAT POLICE GETS --
	-- ADD POLICE SPAWN(CAR) --
end



while true do
	wait(9)	
	choseBH()
	chosePOLICE()
	wait()
end

It seems to be not working in either studio or multiplayer... Can anyone explain why,And debug it please? Greatly appriciate any help.

1 answer

✓ Accepted answer

I see two problems with this immediately.

When you're selecting a player, you're selecting from "the second to the last" (line 11, line 4).

We can also make it a bit shorter by defining a variable for the list of players.

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

(We could then use this as police or bountyHunter).

(Picking from 2 will cause errors if fewer than 2 players are playing)

Note that the above will still error if no one is playing, so there should be a check before continuing (if #players == 0 then)


Secondly, or does not pick randomly between two positions. A or B if A and B are both vectors will just be the first one (A).

To pick randomly, use a short if:

local pos
if math.random(1,2) == 1 then
	pos = game.Workspace.TPTO1.Position
else
	pos = game.Workspace.TPTO2.Position
end

player.Character:MoveTo( pos )

This discussion is preserved as it was. New answers, comments, and votes are closed.