Help with math.max() and math.random()

Hello! So, this is my attempt to make a whole model move to a random position, so basically, the model (which is an apartment) should be positioned absolubtly random, I don’t want any model, near to other model, so yeah, basically, every time a player joins, a apartments spawns, but I want it to be ABSOLUBTLY Random, and very apart from other apartments how would I make that? My attempt:

function check(part, primarypart)
	for i,v in pairs(part:GetChildren()) do
		if v:IsA("Model") then
			check(v, v.Parent.PrimaryPart)
		
		elseif v:IsA("Part") or v:IsA("BasePart") or v:IsA("UnionOperation") then
			local weld = Instance.new("WeldConstraint", primarypart)
			weld.Part0 = v
			weld.Part1 = primarypart
			
		
		end

	end
end

check(script.Parent, script.Parent.PrimaryPart)
wait(1)
script.Parent.PrimaryPart.Anchored = true
local apartments = {}
for i,v in pairs(game.Workspace.Apartments:GetChildren()) do
if v then
	table.insert(apartments, v)
end
end

for i,v in pairs(apartments) do
	if v then
		if (v.PrimaryPart.Position - script.Parent.PrimaryPart.Position).Magnitude >= 40 then
script.Parent.PrimaryPart.Position = Vector3.new(math.random(math.max(1,1123)), -128.709, 146.895)
	elseif not v then
		script.Parent.PrimaryPart.Position = Vector3.new(math.random(math.max(1,1123)), -128.709, 146.895)
		end
end
end

script:Destroy()

Take a look at this: Help with an Agar.io like spawn and generation system - #2 by 1Joeb

You can use seed random numbers with tick() to get random results.

Post is a little different, but same idea.

How I would implement it to mine?

1 Like
--Random number function. 

local SingletonRandom = Random.new(tick())

local function GetRandom(Min,Max)
	return SingletonRandom:NextNumber(Min,Max)
end

So, this is a function that provides a random number between a given min and max. To implement this into your system, instead of saying:

script.Parent.PrimaryPart.Position = Vector3.new(math.random(math.max(1,1123)), -128.709, 146.895)

You would say:

script.Parent.PrimaryPart.Position = Vector3.new(
		
	GetRandom(1, 1123), --Calling the func to get a random X value, passing the min and max. 
	-128.709,  
	146.895		
)

This is assuming the Y and Z of your vector3 are constant values.

Wait, it’s not working for me, how I implement, like my weld, system to this, because it’s not working
:confused:

It’s just moving the primary part, and not model

function check(part, primarypart)
	for i,v in pairs(part:GetChildren()) do
		if v:IsA("Model") then
			check(v, v.Parent.Weld)
		
		elseif v:IsA("Part") or v:IsA("BasePart") or v:IsA("UnionOperation") then
			local weld = Instance.new("WeldConstraint", primarypart)
			weld.Part1 = v
			weld.Part0 = primarypart
			
		
		end

	end
end

	local SingletonRandom = Random.new(tick())


local function GetRandom(Min,Max)
	return SingletonRandom:NextNumber(Min,Max)
end

function LoadApartment(player)
	local plrdata
	local success = pcall(function()
		plrdata = apartments:GetAsync(player.UserId) 
	end)
if success and plrdata then
	--[[local part = Instance.new("Part", workspace)
	part.Name = player.Name
	part.Anchored = true
	part.CanCollide = false
	part.CFrame = 
	print(plrdata["Name"])]]--
	local apartment = game:GetService("ServerStorage")["Apartments"]:WaitForChild(plrdata["Name"], 30):Clone()
	for i,v in pairs(apartment:GetChildren()) do
		if v.Name == "WallColor" then
			v.BrickColor = BrickColor.new(plrdata["ApartmentColor"])
		end
	end
	local SingletonRandom = Random.new(tick())

check(apartment, apartment.PrimaryPart)

apartment.PrimaryPart.Position = Vector3.new(
		
	GetRandom(1, 1123), --Calling the func to get a random X value, passing the min and max. 
	-328.709,  
	146.895		
)
	apartment.Name = player.Name
	apartment.Parent = workspace.Apartments
	apartment["Garage"].Value = tonumber(plrdata["GarageSize"])
else
	NewApartment(player)
end
end