Detect when a character's position's Y value changes

The title says it all, so I won’t be explaining any further for this one.
I’ve tried using the Changed event and GetPropertyChangedSignal but they only work for instances.
Oh and, don’t tell me to listen for position changes because the function will run for X and Z changes too which I don’t really need and will lag my game more.

2 Likes

I’m afraid that whatever you’re looking for does not exist. Could you perhaps define what your character is? Every object in the explorer is an instance. If you mean a BasePart, you can use the character’s HumanoidRootPart. If you’re suggesting that it somehow causes lag to listen to an event, your code is likely to blame.

(Assuming ‘Character’ is already defined)

local root = Character:WaitForChild('HumanoidRootPart')
root:GetPropertyChangedSignal('Position'):Connect(function()
	print(root.Position.Y)
end)
2 Likes

I tried to listen for the Y value’s change with the following code

humrp.Position.Y.Changed:Connect(function()

but it outputted an error because Position.Y is not an instance.
I guess it’s still fine if I listen for position changes instead but i just thought it would fire too often compared to what I thought was possible.

Ah, I see. Neither Position or Position.Y are instances and they are properties. Changed and GetPropertyChangedSignals are built into every BasePart. There’s no way to listen only for the Y value as Position is a Vector3. It wouldn’t make much of a difference anyway.

However, I just remembered that Roblox stopped Position/Orientation-based events from working due to rapid updates. Instead, you’ll have to use a loop.

2 Likes

Why do you need to constantly listen to the Y value?

game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(char)
       local rootpart = char.HumanoidRootPart
       rootpart:GetPropertyChangedSignal("Position"):Connect(function()
           print(player.." is moving to "..rootpart.Position.Y)
       end)
    end)
end)

I want to check when a player reach a certain altitude compared to another part.

As @MightyDantheman stated, you’ll have to use loops or runservice as there is no such event for this scenario.

This would be the best you could get.

You can’t detect property changes of “Position”.

Unfortunately, there is no fancy way of doing this. You can check on HeartBeat to see if the Y coordinate changes.

local RunService = game:GetService("RunService")
local Part = script.Parent

function createYChangedConnection(SubjectPart, callback)
	local lastY = SubjectPart.Position.Y
	
	return RunService.Heartbeat:Connect(function()
		local currentY = SubjectPart.Position.Y
		if currentY ~= lastY then
			callback(currentY)
			lastY = currentY
		end
	end)
end

local Connection = createYChangedConnection(Part, function(y)
	print(y)
end)
1 Like