How to check the position of a moving part?

Every time a function is run, I would like to check the position of an object, which may or may not be moving. It is in a a vehicle, and is basically a radar for certain objects.

The issue is that the “radars” position does not update whenever the function is run. This means the player with the radar may be on a different side of the map, but its comparing its detection to radars original position always.

I haven’t really tried any solutions because I don’t know any, and didn’t get answers from other forums. The code for the position is inside the function, so I thought it would update every time the function was run.

	while true do
	local target = findNearestTorso(script.Parent.humanoidRootPart.Position) 
	if target ~= nil then
               -- flash the green light
	elseif target == nil then
		-- flash the red light
		wait(5)
	end
	  
	
end

Again just to clarify, the rest of the script functions, the only problem is that the position from the code I posted doesn’t update with the radars movement.

Checking the position of a moving part is no different than checking the position of a stationary one. You can just access its current position with Part.Position. I’m a bit confused what you mean by ‘radars’ as the code you’ve shared does not mention anything related to it.

1 Like

I cut out the rest of the code since all of that worked. Basically the part that this script is in, is a radar. It detects all models with a certain name that are within 250 studs. To do this, it takes does math to determine the range between the targets location and the parts location. The problem is in the function I posted. The function works, but the position is not updated when the part moves, even though the function has been run again.

In short, I did do part.position, but the value created isn’t updated when the part moves. Every time the function is run, it keeps that same/old position value it started with.

So, in order to help you, at the very least, I would need to see the code you’re using to track the Position. It sounds to me like you might not be updating the stored position of each part correctly (perhaps because you’re defining a local variable instead of setting a global one).

I think the problem is probably that you’re doing something like this:

local position = part.position

instead of something like this:

part.position

When you store the part’s position in a variable it never updates since it’s storing a value, not a reference.

2 Likes
function findNearestTorso(pos)
	local list = game.Workspace:children()
	local torso = nil
	local dist = 250
	local temp = nil
	local human = nil
	local temp2 = nil
	for x = 1, #list do
		temp2 = list[x]
		if (temp2.className == "Model") and (temp2 ~= script.Parent)			
		then
			temp = temp2:findFirstChild("HumanoidRootPart")
			human = temp2:findFirstChild("Humanoid")
			if (temp ~= nil) and (human ~= nil) and (human.Health > 0) then
				if (temp.Position - pos).magnitude < dist then
					torso = temp
					dist = (temp.Position - pos).magnitude
				end
			end
		end
	end
	return torso
end



	
while true do
	local target = findNearestTorso(script.Parent.humanoidRootPart.Position)
	if target ~= nil then
-- green light
	elseif target == nil then
-- red light
	end
	  
	
end

When you get the position of a moving part, you get its position at the time it is checked instead of a constantly updated one. If you wanted to constantly get its position then you would need to use ‘part.Position’ as frequently as you need the coordinates.

It’s worth mentioning that this is how real life radars work, they send out “messages” every couple of seconds and see if they get a location ping back.

How do I do that though? I thought that’s what I did by putting it under the “while true” since it would constantly repeat that function, and which would update the position.

“while true do” would mean that the game is updating the position as fast as it can load, which could mean that the position is changing too fast for your other script to keep up with. Try adding a wait(0.1) and seeing if it resolves the issue.

There is no other script, everything is in this script. And it just never detects a position change at all, I did :print to check.There is also a wait inside the function, so it doesn’t instantly check the position each time.

try adding wait() instead of true in your while true do statement

Found your issue, you defined “HumanoidRootPart” as part of the player when it is actually part of the character model. Within a local script, you would get the position of the character with the line below:

local target = game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame.Position

Or if you’re using a non-local script:

local target = game.Players.PLAYERUSERNAMEVARIABLEHERE.Character.HumanoidRootPart.CFrame.Position
1 Like

Well its not a character/player, its AI.

To add on if you want to only check for the position every X seconds or so, you can use:
RunService.Heartbeat for the client,
RunService.RenderStepped for the server.

I forgot what the math is but you can use the argument deltatime to check if enough time has passed probably.

Well its not a character/player, its AI.

I don’t know how your game is setup, but you’re gonna have to create a way to distinguish AI’s and players, maybe a specific property/value you might have or some sort.

1 Like

Read second line. I’m not sure how your AI works but it has to return a variable that gives the username of the player it wants the position of.

None of this involves the player though. It finds an enemy soldier AI, and it runs automatically. All of that detection works, its just that the script.Parent.humanoidRootPart.Position doesn’t update its position every time the findNearestTorso function is run.

So it’s an AI finding an AI…?ㅤㅤㅤㅤㅤㅤㅤㅤㅤㅤ

Yes, which means it wouldn’t find a player/character.