Making randomized positions more scattered

I made a script that chooses random positions behind the player for minions to stand in, but I wanted to make it so they can’t stand too near to each other. I put it in the repeat-until loop but it doesn’t seem to work, as they are still overlapping.


local part = script.Parent
local debounce = true

part.Touched:Connect(function(otherPart)
	local character = otherPart.Parent
	local humanoid = character:FindFirstChild("Humanoid")
	if humanoid and debounce == true then
		local player = game.Players:GetPlayerFromCharacter(humanoid.Parent)
		if player then
			local head = character.Head
			local positions = {}
			positions[1] = head.Position
			for i, v in pairs(game.Workspace.Folder:GetChildren()) do
				local magnitude
				local position
				local pos = #positions+1
				local random
				repeat
					repeat random = math.random(-1,1) until random ~= 0
					local X = math.sign(head.CFrame.LookVector.X)*24*math.random()*random 
					local Z = head.CFrame.LookVector.Z*math.random()*(-24)
					position = head.Position + Vector3.new(X,1,Z)
					positions[pos] = position
					for a,b in ipairs(positions) do
						magnitude = (position - b).Magnitude
						if magnitude > 8 then break end
					end
					if magnitude < 8 then table.remove(positions, pos) end
				until magnitude > 8
				v.Humanoid:MoveTo(position)
			end
			debounce = false
		end
	end
end)

You can change the random seed of math.random.

math.randomseed(tick()) -- completely random randomseed since tick() is a constantly changing number

while wait(Interval) do
    -- gives a random position
    local RandomX = {math.random(-24, -5), math.random(5, 24)} -- changes the threshold
    local RandomZ = {math.random(-24, -5), math.random(5, 24)}
    local Position = Vector3.new(RandomX[math.random(1, 2)], YPos, RandomY[math.random(1, 2)])
end

What is the math.randomseed(tick()) for in this script?

1 Like

It sets the seed for the pseudo-number generator. It makes numbers more “random.”

2 Likes

Ok and do I need the wait(Interval)? I want to get few positions at once.

Oh no you don’t need the wait at all, you can put it in a loop without an interval and get the same results.

1 Like