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

This code seems repetitive and will take very long to code, how can I make this shorter?

Asked by 4 years ago
Edited 4 years ago
local rs = game:GetService('ReplicatedStorage')
local entities = rs:WaitForChild('Entities')

local zombie = entities.Zombies:WaitForChild('Zombie')
local zombieFast = entities.Zombies:WaitForChild('Fast Zombie')

local randomNumber = math.random(1,50)
if randomNumber == 1 then
    local clone = zombie:Clone()
    clone.Parent = workspace:WaitForChild('Zombies')
    clone.HumanoidRootPart.CFrame = CFrame.new(math.random(-125, 125), 15, math.random(-125, 125))
elseif randomNumber == 2 then
    local cloneFast = zombieFast:Clone()
    cloneFast.Parent = workspace:WaitForChild('Zombies')
    cloneFast.HumanoidRootPart.CFrame = CFrame.new(math.random(-125, 125), 15, math.random(-125, 125))
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?

0
You can use a function for spawning zombies. Like this SpawnZombie(ZombieType) Block_manvn 395 — 4y

2 answers

Log in to vote
1
Answered by 4 years ago

Try this and test if it works. I wasn't able to test so I can't guarantee that it works.

function SpawnZombie(ZombieType)
    local zombie = ZombieType:Clone()
    zombie.Parent = workspace:WaitForChild('Zombies')
    zombie.HumanoidRootPart.CFrame = CFrame.new(math.random(-125, 125), 15, math.random(-125, 125))
end

local Zombie = ""

local randomNumber = math.random(1,50)
if randomNumber == 1 then
    Zombie = "zombie"
    SpawnZombie(Zombie)
elseif randomNumber == 2 then
    Zombie = "zombieFast"
    SpawnZombie(Zombie)
end
0
22:54:45.259 - Players.Bloxulen.PlayerGui.SpawnerGUI.Scripts.MainScript:13: attempt to call a nil value Bloxulen 88 — 4y
0
on local zombie = ZombieType:Clone() Bloxulen 88 — 4y
0
nvm fixed Bloxulen 88 — 4y
Ad
Log in to vote
1
Answered by 4 years ago

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.

local function CreateZombie(Zombie, RootCFrame)
    local Clone = Zombie:Clone()
    Clone:WaitForChild("HumanoidRootPart").CFrame = RootCFrame
    Clone.Parent = game.Workspace:WaitForChild("Zombies")
    return Clone
end

local RandomNumber = math.random(1,50)
if RandomNumber == 1 then
    local clone = CreateZombie(zombie, CFrame.new(math.random(-125, 125), 15, math.random(-125, 125)))
end

I hope you get the idea

Answer this question