Math.random started generating the same numbers

This issue popped out of nowhere and has been driving me crazy. I have a script that spawns 100 NPCs around the map of my game. It has been working fine, but randomly (without changing the code whatsoever) it started spawning all the NPCs at the same position. I’ve tried many different ways of fixing it such as using Random.new() or math.randomseed(), but neither worked. Here is my script:

local dummy = workspace.NPCs:GetChildren()[1]
dummy.Parent = nil

function randomCF()
	return CFrame.new(math.random(-304, 1742), 4, math.random(-1014, 995)) * CFrame.Angles(0, math.rad(math.random(0, 359)), 0)
end

function createDummy(name)
	local new = dummy:Clone()
	new.Name = name
	new.HumanoidRootPart.CFrame = randomCF()
	new.Parent = workspace.NPCs
	return new
end

for i = 1, workspace:GetAttribute("NPCCount") do
	createDummy("Player "..tostring(i))
end

You could try adding math.randomseed(tick()) at the top of your code or something. I think this is a problem that randomly happens and solves itself randomly.

1 Like

I added that to the top of the randomCF() function, didn’t work. I’ll try it at the top my code and see if that works.

Aw, unfortunately didn’t work… :frowning:

1 Like

Hey, I believe this issue is related to the fact that your are spawning them all at once - I believe random numbers are linked to the time they are generated.

Try this:

for i = 1, workspace:GetAttribute("NPCCount") do
	createDummy("Player "..tostring(i))
	wait(0.05)
end

Alright, I tried adding a wait already, but didn’t work. I haven’t tried with a wait of 0.05 though so I guess I’ll try this.