Restricting along the Y axis in a Vector3

So I’ve been making a randomly generated obby, and I keep having the parts keep going down too far, and I’m wondering if there is a way to restrict how low they can get put. It would be optimal if it wasn’t a huge amount of code required, but I don’t really mind all that much. I’m currently using :

local partTypes = {workspace.SlippyParts, workspace.NormalParts, workspace.KillParts, workspace.FallingParts}
local Colors = {BrickColor.new("Baby blue"), BrickColor.new("Medium stone grey"), BrickColor.new("Really red"), BrickColor.new("Cashmere")}

local maxGap = 4
local maxUpChange = 4
local maxDownChange = 10
local maxXZChange = 5
local maxJumps = 50

local JumpNum
local End
local lastNum = 3

function NewPart(PastPart)
	local TypeNum = math.random(#partTypes)
	local Part = Instance.new("Part")
	
	Part.Size = Vector3.new(4, 1, 4)
	Part.Anchored = true
	
	if TypeNum == 3 and lastNum ~= 3 then
		Part.CanCollide = false
	end
	if lastNum == 3 and TypeNum == 3 then
		TypeNum = 2
	end
	
	Part.Parent = partTypes[TypeNum]
	Part.BrickColor = Colors[TypeNum]
	
	local things = {1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, -1, -1, -2, -2, -3, -4, -6, -7, -8, -9, -10}
	Part.Position = PastPart.Position + Vector3.new(2 + math.random(-1, maxXZChange) + math.random(maxGap), things[math.random(#things)], 2 + math.random(-maxXZChange, maxXZChange))
	
	if JumpNum > (#partTypes[1]:GetChildren() + #partTypes[2]:GetChildren() + #partTypes[3]:GetChildren() + #partTypes[4]:GetChildren()) then
		wait(.1)
		NewPart(Part)
	else
		print("Done")
		End = Part
	end
end

local function obby()
	JumpNum = math.random(25, maxJumps)
	NewPart(workspace.Start)
end

obby()

I tried doing Part.Position.Y = math.Clamp(etc.), but that didn’t work as Y is not assignable. Any help would be appreciated, have a nice day.

Also the things variable, is simply because it always would favor going down if I didn’t do it that way.

You’re close, inorder to do what is required, you need to give it a Vector3 in the Position property and just clamp the Y there.

Example

local y = 15

part.Position = Vector3.new(part.Position.X,math.clamp(y,0,10),part.Position.Z)

This would put the part at 10 on the y axis because 10 is the max value, XYZ properties of a Vector3 are Read only, they cannot be assigned as you have noticed

I Don’t know why I didn’t think of that :man_facepalming: Thanks :slight_smile:

1 Like

Its okay! Sometimes the simplest issues go over your head and require another person to help you figure it out, doesn’t matter so long as in the end you get your answer!

If you have anymore issues don’t be afraid to make another post!