Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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?

1 comment

0

You can use a function for spawning zombies. Like this SpawnZombie(ZombieType)

2 answers

✓ Accepted answer

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

3 comments

0

22:54:45.259 - Players.Bloxulen.PlayerGui.SpawnerGUI.Scripts.MainScript:13: attempt to call a nil value

0

on local zombie = ZombieType:Clone()

0

nvm fixed

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

This discussion is preserved as it was. New answers, comments, and votes are closed.