Whats a method to check if a parts position reached a certain point without using a While loop and in a seerver script?

title title title title title title title

i tried renderstepped but didnt work, is there any alternitives for this?

check the part’s magnitude (distance), constantly (loop)

how would you Do this?aaaaaaaaaaa

Part.Changed:Connect(function()
If Part.Position == targetPosition then
–code
end
end

1 Like

i tried this but the changed event isnt firing, maybe cause im using a constraint to move it

image
image
image

it fires randomly at start and stops, i’d need it to fire everytime it changed to detect when its finished moving

Without while loop and server script

local RunService = game:GetService("RunService")

-- Define the target position
local targetPosition = Vector3.new(0, 10, 0)

-- Define the part you want to track
local part = workspace.Part

-- Define a threshold distance to determine when the part has reached the target
local threshold = 1 -- You can adjust this value

local function checkPosition()
    local distance = (part.Position - targetPosition).magnitude
    if distance <= threshold then
        print("The part has reached the target position!")
        -- Disconnect the event after the condition is met
        connection:Disconnect()
    end
end

-- Connect the function to the Heartbeat event
local connection = RunService.Heartbeat:Connect(checkPosition)
1 Like

Thats good, but ideally we should find a method that works without loops at all.

1 Like

Try getPropertyChangedSignal(), suposedly it has similar performance as changed().

1 Like

would this pause other proccesses in the script? aaaa

Something like this ?

		Elevator.PrimaryPart:GetPropertyChangedSignal("CFrame"):connect(function()
			print("a")
		end)

No, the code would not pause other processes in the script. It leverages the RunService.Heartbeat event, which runs a function every frame without blocking other processes.

1 Like

Sure, alternatively you could use a tween. Just set the time parameter to distance/speed.

1 Like

Thank you seems to be working but how would i disconnect it if the variable starts it afterwards?

		local function checkPosition()
			
			local distance = (Elevator.PrimaryPart.Position.Y - Elevator.PrimaryPart:GetAttribute(RequestedFloor.Name))
			print(distance, Elevator.PrimaryPart:GetAttribute("CurrentFloor"))
			
			if distance <= 0 then
				connection:Disconnect()
				elevatorModule.OpenDoors(Elevator)
				Elevator.PrimaryPart:SetAttribute("CurrentFloor", RequestedFloor.Name)
			end
			
		end
		
		local connection = RunService.Heartbeat:Connect(checkPosition)
		

RenderStepped doesn’t work in servers, it’s only cliend-sided, however, you can use either Stepped or Heartbeat

2 Likes
local function checkPosition()

	local distance = (Elevator.PrimaryPart.Position.Y - Elevator.PrimaryPart:GetAttribute(RequestedFloor.Name))
	print(distance, Elevator.PrimaryPart:GetAttribute("CurrentFloor"))

	if distance <= 0 then
		elevatorModule.OpenDoors(Elevator)
		Elevator.PrimaryPart:SetAttribute("CurrentFloor", RequestedFloor.Name)
		
		return "Disconnect"
	end

end

local connection = RunService.Heartbeat:Connect(function(deltaTime)
	local connector = checkPosition()
	
	if connector == "Disconnect" then
		connection:Disconnect()
	end
end)
1 Like

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