01 | local rs = game:GetService( 'ReplicatedStorage' ) |
02 | local entities = rs:WaitForChild( 'Entities' ) |
03 |
04 | local zombie = entities.Zombies:WaitForChild( 'Zombie' ) |
05 | local zombieFast = entities.Zombies:WaitForChild( 'Fast Zombie' ) |
06 |
07 | local randomNumber = math.random( 1 , 50 ) |
08 | if randomNumber = = 1 then |
09 | local clone = zombie:Clone() |
10 | clone.Parent = workspace:WaitForChild( 'Zombies' ) |
11 | clone.HumanoidRootPart.CFrame = CFrame.new(math.random(- 125 , 125 ), 15 , math.random(- 125 , 125 )) |
12 | elseif randomNumber = = 2 then |
13 | local cloneFast = zombieFast:Clone() |
14 | cloneFast.Parent = workspace:WaitForChild( 'Zombies' ) |
15 | cloneFast.HumanoidRootPart.CFrame = CFrame.new(math.random(- 125 , 125 ), 15 , math.random(- 125 , 125 )) |
16 | end |
So on and so forth what I'm saying is this will take a while to do for each individual zombie and I was just wondering if there was a way to make this shorter?
Try this and test if it works. I wasn't able to test so I can't guarantee that it works.
01 | function SpawnZombie(ZombieType) |
02 | local zombie = ZombieType:Clone() |
03 | zombie.Parent = workspace:WaitForChild( 'Zombies' ) |
04 | zombie.HumanoidRootPart.CFrame = CFrame.new(math.random(- 125 , 125 ), 15 , math.random(- 125 , 125 )) |
05 | end |
06 |
07 | local Zombie = "" |
08 |
09 | local randomNumber = math.random( 1 , 50 ) |
10 | if randomNumber = = 1 then |
11 | Zombie = "zombie" |
12 | SpawnZombie(Zombie) |
13 | elseif randomNumber = = 2 then |
14 | Zombie = "zombieFast" |
15 | SpawnZombie(Zombie) |
16 | end |
I made a quick idea for how to shorten this code, you can use a function that takes model and position parameters and returns the clone, making your code shorter.
01 | local function CreateZombie(Zombie, RootCFrame) |
02 | local Clone = Zombie:Clone() |
03 | Clone:WaitForChild( "HumanoidRootPart" ).CFrame = RootCFrame |
04 | Clone.Parent = game.Workspace:WaitForChild( "Zombies" ) |
05 | return Clone |
06 | end |
07 |
08 | local RandomNumber = math.random( 1 , 50 ) |
09 | if RandomNumber = = 1 then |
10 | local clone = CreateZombie(zombie, CFrame.new(math.random(- 125 , 125 ), 15 , math.random(- 125 , 125 ))) |
11 | end |
I hope you get the idea