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