Spawn Chance script

I was wondering if there is another way to spawn a object based on a %chance

I am currently using a r = math.random(1,100) and with a for loop for every spawn point i check if the number is in the range to be spawned
Example

if r > 0 and r <= 20 then -> Spawn Object 1
elseif r > 20 and r <= 40 then -> Spawn Object 2

and so on for them all

NOTE: Since this is a spawn point i only want one object to spawn

1 Like

Uhm… like this?

game.Players.ayerAdded:Connect(function(plr)
   plr.CharacterAdded:Connect(function()
      local c = math.random(1,2)
      if c == 1 then
         plr.Character.HumanoidRootPart.CFrame = workspace.Spawn1.CFrame
      elseif c== 2 then
      --etc...
   end)
end)

Yes but using the math.random 1,100 i emulate a %chance so

if r > 0 and r <= 20 then -> Spawn Object 1
This will be equal to a 20% chance

if r > 20 and r <= 100 then -> Spawn Object 2
This one will be equal to a 80% chance

The one you posted just spawns them randomly

Is only do the same but with others chances

if c => 1 and c < 10 then -- only example

While I don’t think theres any other way, you can shorten it by doing this:

if r <= 20 then
elseif r <= 40 then

as the second elseif will only run if the first is false.

1 Like