Randomized Models Won't Spawn [Solved]

Hello, I am trying to make a part that randomizes which model to place from ReplicatedStorage, but it either turns disappears or doesn’t change and I don’t know why it doesn’t place the model?

generate = math.random(1,6)

if generate == 1 then
	
end

if generate == 2 then
	decor = game.ReplicatedStorage.ForestDecor.Campfire:Clone()
	decor.Position = script.Parent.Position
	decor.Parent = game.Workspace
end

if generate == 3 then
	decor = game.ReplicatedStorage.ForestDecor.DoubleTrees:Clone()
	decor.Position = script.Parent.Position
	decor.Parent = game.Workspace
end

if generate == 4 then
	decor = game.ReplicatedStorage.ForestDecor.Rock:Clone()
	decor.Position = script.Parent.Position
	decor.Parent = game.Workspace
end

if generate == 5 then
	decor = game.ReplicatedStorage.ForestDecor.Tree:Clone()
	decor.Position = script.Parent.Position
	decor.Parent = game.Workspace
end

if generate == 6 then
	decor = game.ReplicatedStorage.ForestDecor.TripleTree:Clone()
	decor.Position = script.Parent.Position
	decor.Parent = game.Workspace
end

script.Parent:Destroy()

Note I am really new to scripting.

1 Like

May be this line.
Should be decor:SetPrimaryPartCFrame(script.Parent.CFrame)
or another potential, not recommended way is decor:MoveTo(script.Parent.Position)

2 Likes

This is quite irrelevant, but still helpful to developers.

Instead of having multiple if statements, have one if statement with more elseifs. I might be incorrect but this could increase server performance as it doesn’t have to do multiple if chunks.

2 Likes

Like what @PoppyandNeivaarecute mentioned, there is no Position property in models. Instead, you can run :SetPrimaryPartCFrame(script.Parent.CFrame) instead which changes the CFrame of the model.

Furthermore, you can clean up the code by utilizing a dictionary which indexes each cloneable decoration with a number.

local RS = game:GetService("ReplicatedStorage")

local generate = math.random(1, 6)
local list = {
	[2] = RS.ForestDecor.Campfire,
	[3] = RS.ForestDecor.DoubleTrees,
	[4] = RS.ForestDecor.Rock,
	[5] = RS.ForestDecor.Tree,
	[6] = RS.ForestDecor.TripleTree
}

if list[generate] then
	local decor = list[generate]:Clone() do
		decor.Parent = workspace
		decor:SetPrimaryPartCFrame(script.Parent.CFrame)
	end
end
script.Parent:Destroy()
1 Like
local RS = game:GetService("ReplicatedStorage")
local list = {
	RS.ForestDecor.Campfire,
	RS.ForestDecor.DoubleTrees,
	RS.ForestDecor.Rock,
	RS.ForestDecor.Tree,
	RS.ForestDecor.TripleTree
}

local decor = list[math.random(1,#list)]:Clone()
decor.Parent = workspace
decor:MoveTo(script.Parent.Position)
script.Parent:Destroy()

made it a wee bit better.

2 Likes

If you want each decoration to be assigned for a specific number, use my solution. Otherwise, use @Qinrir’s solution which completely choses a random one.

1 Like

Only thing to add: SetPrimaryPartCFrame has a bug that eventually causes all the parts within the model to be displaced from their original position relative to the Primary Part. Roblox now recommends PivotTo as the new model placing command. Works the same.

2 Likes