How can I compare positions using <=

Trying to see if the player is below a certain y axis

I have also tried doing pos1 as the entire position and comparing to a vector3.new(humrp.Position.X, (lowest y axis), humrp.Position.Z)

local Pos1 = humrp.Position.Y

			

			if Pos1 <= -10 then

				local origin = humrp.Position
				local direction = humrp.CFrame.UpVector

				local raycastResult = workspace:Raycast(origin, direction)

				if raycastResult then
					print(raycastResult.Position)
					print(raycastResult.Instance)
				end

You can compare using the Vector3.Y.

Compare the Y values.

if Pos1.Y >= Pos2.Y then --If a certain Y is higher than another Y.

end

I don’t see anything wrong here. Doesn’t that work?

No, it doesn’t seem to print the ray cast results Edit: ray cast should be looking up

Add a print after the if statement, and check if it is printed:

local Pos1 = humrp.Position.Y

			

			if Pos1 <= -10 then
				print("Y is lower than 10")
				local origin = humrp.Position

If it is printed, the issue is not with the height.

Doesn’t print anything, and I set the Y axis position to -10 on the server

When does this code run? Can share some of the code above it?

game.Players.PlayerAdded:Connect(function(player)


	player.CharacterAdded:Connect(function(character)

		local humrp = character:WaitForChild("HumanoidRootPart")

		while wait(20) do

			local Pos1 = humrp.Position.Y

			

			if Pos1 <= -10 then
				print("Y is less then -10")

				local origin = humrp.Position
				local direction = humrp.CFrame.UpVector

				local raycastResult = workspace:Raycast(origin, direction)
				print("Created Ray cast")

				if raycastResult then
					print(raycastResult.Position)
					print(raycastResult.Instance)
				end

			end
		end

	end)

end)


Edit: oh wait a second LOL

while wait(20) waits 20 seconds and THEN runs… That means you’re code below won’t run until 20 seconds has passed, then it’ll do that & loop.

Yeah, I didn’t remember I had that in there lol

1 Like

maybe try lengthening the direction by doing (direction * 100)

1 Like

I doubt this does anything, as OP wasn’t waiting for the while wait(20) do loop, meaning the tween didn’t play at all.

1 Like

It seems to be detecting my hat’s handles how can I avoid that?

Fixed that, but it keeps detecting my handle in my accessories

Use RaycastParams, and set the FilterDescendantsInstances’s property to your character in a table.

Awesome, I got the closet part but what if I want the highest part, incase it is like a mountain

you would have to cast a ray downwards from a higher position

local origin = humrp.Position + Vector3.new(0, 100, 0) --however high up you need to start
local direction = Vector3.new(0, -100, 0) -- point down 100 studs

--cast - get your results - the first thing hit will be the 'highest' object or nil if you've filtered out your player's character in RaycastParams - meaning there's nothing above you.

by the way - just so you know, doing:

local direction = humrp.CFrame.UpVector

will point upwards relative to your player’s character.

This means, if the player falls over for any reason and the cast is made - the ray won’t point straight up, but in whichever direction the top of your character is facing.

Hence why I swapped in the Vector3.new(0, -100, 0)

but if you need relativeness that is fine then.

Finally, I think LookVector / UpVector might be a Unit - (e.g. - V3.new(0, 100, 0) == V3.new(0, 1, 0) - so if you need the ray to cast for a longer distance, you’ll like need to multiply by the distance you need.

local direction = humrp.CFrame.UpVector.Unit * 100 -- definitely traveling 100 studs above the character's head

This is important for ray casts as the ray will only travel as far as the magnitude of the direction value.

local Part1 = workspace.Part1
local Part2 = workspace.Part2
local Distance = 10000 -- change this to ur distance thing
if (Part1.Position - Part2.Position).Magnitude <= Distance then
	print((Part1.Position - Part2.Position).Magnitude) -- change this to the thing u want to happen
end

should work i think

I accidentally put a wait 20 in the loop lol