Help with math.random

Hey, so I’m trying to make different animals spawn which are in a folder I hold in ServerStorage and its working but the problem is that, Im picking two random animals from the folder, and I have two in there but when it picks the same animal twice it only puts one in workspace but when its different ones it works flawlessly

image

WORKS ( Spawns 1 of each animal ) : image

Doesnt work ( Spawns animal only once ): image
Code :

local animalsFolder = game.ServerStorage.animals:GetChildren()
local randomSpawns = game.Workspace:FindFirstChild("defaultMap").animalSpawns:GetChildren()
local function placeRandomly(obj,pos)
	obj.Parent = workspace
	obj:SetPrimaryPartCFrame(pos)
	print("Spawned "..obj.Name)
end
placeRandomly(animalsFolder[math.random(1,#animalsFolder)],randomSpawns[math.random(1,#randomSpawns)].CFrame)
placeRandomly(animalsFolder[math.random(1,#animalsFolder)],randomSpawns[math.random(1,#randomSpawns)].CFrame)
local function placeRandomly(obj,pos)
    local newObj = obj:Clone()
	newObj.Parent = workspace
	newObj:SetPrimaryPartCFrame(pos)
	print("Spawned "..obj.Name)
end

You need to clone the object each time to spawn it!

Dang omg I forgot about :Clone() :man_facepalming:

@CheekySquid Thanks LOL
@Thereasonableplayer Thanks just realized LOL

1 Like

aaaa I see the issue here. Your issue is a mistake that I keep making lol. You gotta clone the pig in order to keep referencing it from the folder. Your problem is that you just put it in the workspace, but then nothing is in the folder now. Makes sense?

1 Like

no worries bud I do the same thing all the time and end up confused.

1 Like