For loop won't count correctly

I’m making a item spawning system and for some reason my for loop does double the amount of spawning than it should.

If ItemLimit is equal to 5, then it would spawn 10 items. The spawning function isn’t causing the problem though. I tried putting the third argument of the for loop to 2 to fix it, but I realized it wouldn’t work on odd numbers.

local function spawnItems(area, amount)
	for i = 1, amount, 1 do
		local xVectorRandom = area.CFrame.RightVector * area.Size.X * (math.random(-50, 50) / 100)
		local yVectorRandom = area.CFrame.UpVector * area.Size.Y
		local zVectorRandom = area.CFrame.LookVector * area.Size.Z * (math.random(-50, 50) / 100)
		local spawnPosition = area.CFrame + xVectorRandom + yVectorRandom + zVectorRandom
		local clonedItem = items[ChooseItemBasedOnRarity(area.ItemsToSpawn:GetChildren())]:Clone()
		clonedItem.PrimaryPart = clonedItem:FindFirstChild("Primary")
		clonedItem:SetPrimaryPartCFrame(spawnPosition)
		clonedItem.Parent = area.ItemsInArea
	end
end

local function startSpawns()
	for i, area in pairs(workspace.Areas:GetChildren()) do
		spawnItems(area, area.ItemLimit.Value)
	end
end

startSpawns()
1 Like
local function spawnItems(area, amount)
	for i = 1, amount do
		local xVectorRandom = area.CFrame.RightVector * area.Size.X * (math.random(-50, 50) / 100)
		local yVectorRandom = area.CFrame.UpVector * area.Size.Y
		local zVectorRandom = area.CFrame.LookVector * area.Size.Z * (math.random(-50, 50) / 100)
		local spawnPosition = area.CFrame + xVectorRandom + yVectorRandom + zVectorRandom
		local clonedItem = items[ChooseItemBasedOnRarity(area.ItemsToSpawn:GetChildren())]:Clone()
		clonedItem.PrimaryPart = clonedItem:FindFirstChild("Primary")
		clonedItem:SetPrimaryPartCFrame(spawnPosition)
		clonedItem.Parent = area.ItemsInArea
	end
end

local function startSpawns()
	for i, area in pairs(workspace.Areas:GetChildren()) do
		spawnItems(area, area.ItemLimit.Value)
	end
end

startSpawns()

This is basically the exact same thing as setting the 3rd argument in the for loop to 1. It still doesn’t work and it spawns double the amount.

Yes I noticed this
hmm put a print function and see how many will print

2 Likes

i found out that another script was doing something to that script, thanks!

1 Like