Blocks are in tables and I was wondering how to simplify this code so that I don't have to write it over and over again:
rs = game.ReplicatedStorage local block = { rs:WaitForChild("StoneBlock"), rs:WaitForChild("DimondBlock") } local dimondChance = 0.1 local rand = math.random() if rand < dimondChance then local clonedblock = block[2]:Clone() clonedblock.Parent = folder --destination folder clonedblock:SetPrimaryPartCFrame(CFrame.new(x, y, z)) else local clonedblock = block[1]:Clone() clonedblock.Parent = folder --destination folder clonedblock:SetPrimaryPartCFrame(CFrame.new(x, y, z)) end
Thanks for the Help!
When code is repeated like this you can pull the repeated code out into a function and just pass in the parts that change as variables. Something like this:
rs = game.ReplicatedStorage local block = { rs:WaitForChild("StoneBlock"), rs:WaitForChild("DimondBlock") } local dimondChance = 0.1 local function CreateBlock(blockType) local clonedblock = blockType:Clone() clonedblock.Parent = folder --destination folder clonedblock:SetPrimaryPartCFrame(CFrame.new(x, y, z)) end local rand = math.random() if rand < dimondChance then CreateBlock(block[2]) else CreateBlock(block[1]) end