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

How to randomize enabling scripts on a script using math.random?

Asked by 3 years ago
Edited 3 years ago

So me and my friend are making a helicopter game and we want to make only one random helicopter to chase the player when we play the game.

game.Players.PlayerAdded:connect(function(plr)
    math.random(Spawner1, Spawner2, Spawner3)
    script.Parent.Spawner1.Station.heli.Fighter.ChaseScript.Disabled = false
    script.Parent.Spawner2.Station.heli.Fighter.ChaseScript.Disabled = true
    script.Parent.Spawner3.Station.heli.Fighter.ChaseScript.Disabled = true
end)

0
only one helicopter spawns for now but i want to randomize the script that should be disabled MrMonthh 11 — 3y

3 answers

Log in to vote
1
Answered by
raid6n 2196 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

You're doing the math.Random wrong.

You can only do math.random with numbers, try this:

game:GetService("Players").PlayerAdded:Connect(
    function(plr)
        local random = math.random(1, 3)
        if random == 1 then
            script.Parent.Spawner1.Station.heli.Fighter.ChaseScript.Disabled = false
        elseif random == 2 then
            script.Parent.Spawner2.Station.heli.Fighter.ChaseScript.Disabled = false
        elseif random == 3 then
            script.Parent.Spawner3.Station.heli.Fighter.ChaseScript.Disabled = false
        end
    end
)


Another way you could do this is getting a random string from a table and getting a random string from it using math.random. Here's the code:

local table = {"Spawner1", "Spawner2", "Spawner3"

local random = math.random(1, #table) -- random string from table
if random then
    script.Parent:FindFirstChild(random).Station.heli.Fighter.ChaseScript.Disabled = false
end

1
or you know `script.Parent["Spawner"..math.random(3)].Station.heli.Fighter.ChaseScript.Disabled` which is just one line and use `Connect` instead of `connect` and game.Players should be a service VerdommeMan 1479 — 3y
0
all of the helicopters chase after you, thanks for trying to help. MrMonthh 11 — 3y
Ad
Log in to vote
0
Answered by
Nckripted 580 Moderation Voter
3 years ago

So first, we need to get a random number:

local randomNum = math.random(1,3)

Note that Roblox random numbers are not that random, so you might have the same number multiple times.

Now we need to disable every script to only pick one:

script.Parent.Spawner1.Station.heli.Fighter.ChaseScript.Disabled = true
script.Parent.Spawner2.Station.heli.Fighter.ChaseScript.Disabled = true
script.Parent.Spawner3.Station.heli.Fighter.ChaseScript.Disabled = true

Now we'll add if statements and depending on the number chosen, we will activate a script:

if randomNum == 1 then
    script.Parent.Spawner1.Station.heli.Fighter.ChaseScript.Disabled = false
elseif randomNum == 2 then
    script.Parent.Spawner2.Station.heli.Fighter.ChaseScript.Disabled = false
else
    script.Parent.Spawner3.Station.heli.Fighter.ChaseScript.Disabled = false
end 

--I put an else because if the number is not 1 or 2 then it has to be 3

Replace the code inside the PlayerAdded with this code.

Hope this helped!

Log in to vote
0
Answered by 3 years ago

Use a table with math.random.

local possibleScripts = {
script.Parent.Spawner1.Station.heli.Fighter.ChaseScript,
script.Parent.Spawner2.Station.heli.Fighter.ChaseScript,
script.Parent.Spawner1.Station.heli.Fighter.ChaseScript,
}
game.Players.PlayerAdded:connect(function(plr)
          local rand = possibleScripts[math.random(1, #possibleScripts)] -- choose a random
          rand.Disabled = true
          for _, scr in pairs(possibleScripts) do
                if scr ~= rand then
                      scr.Disabled = true
                end
          end
end)

Be Careful, your script will disable all other Helicopters and enable those. Just a warning.. If this helps, please mark me as a soloution!

Answer this question