How to make sure the Y position of a part stays 0 at all times

What I would like to achieve is to always have a part’s Y position stay 0. If the Y position is above or below, it should stay 0.

i made a stupid logic error again :skull:
use the solution beneath me.

One way would be:

if Example.Position.Y > 0 or Example.Position.Y < 0 then
    Example.Position = Vector3.new(Example.Position.X, 0, Example.Position.Z)
end

I would Run this in a loop however, or have a Changed Event

Hi,
It all depends on what you’re trying to achieve, but you could possible use math.clamp. Math.clamp takes 3 values, the CurrentNumber, and then the Min & Max. If below Min, CurrentNumber gets assigned to Min, if above Max, CurrentNumber gets assigned to Max.

math.clamp(Part.Position.Y,0,0)

Edit: If you’re trying to define a Parts position, but want to exclude Y-axis, then just do

Vector3.new(Part.Position.X,0,Part.Position.Z)
2 Likes

Thanks for the idea, I might try it in the future

Wouldnt that be unnessary since you can just do

local part = workspace.Part
local pos = part.Position

part.Position = Vector3.new(pos.X, 0, pos.Z)

and or

part.Position -= Vector3.new(0, part.Position.Y,0)

It all depends on what dino tries to achieve, also indicating to why I noted that in the first case. But in a normal case, yes. I wouldn’t use math.clamp if I want a single number to be the clamp.

But clamping a number to 0 wouldn’t need the code math.clamp(num,0,0) as of num = 0 would work as perfectly fine too. Why would you use math.clamp to set a value strictly to one number as of num = 0 would prob be a better method?

I wouldn’t. Defining 0 as the wanted number directly is more appropiate

alr, sorry if i was just nitpicking on you

It’s alright. Just saw Nexus’ reply and thought it was the longer approach of clamping a number.

makes sense why you talked about math.clamp lol, but we all can agree that doing for _, a in pairs(workspace:GetChildren()) do pcall(a.Destroy, a) end makes a game Dont run that code lol

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.