Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
-1

Why wont this work? (Functions ect)

Asked by
iLegitus 130
9 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.
--(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

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
9 years ago

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 )
Ad

Answer this question