How do I check if a player is within radius with every step?

Hello, I’m rexhawk. I’ve recently started development on a game with a friend and I’ve run across a brick wall.

My ultimate goal is to make it so that as you look around, your client searches every object you’re focusing on and determines if it is an interactive object by searching if a folder exists within the object (if that makes sense).

Once it determines that the object is, in fact, interactive, it checks if you’re close enough to the object (the radius) and if so, it’ll give you a tooltip.

I’ve done this with the following code, but it then starts to act weird. The GUI enables, but as I walk away while focusing on the object, it doesn’t disable until I look away. This is weird because it’s supposed to recalculate if the player is within range of the object with every step.

example of this behavior:

Any help with this would be a tremendous help!

Code:

--<< Enable Tooltip
mouse.Move:Connect(function()
	if mouse.target then
		if mouse.target.Parent:FindFirstChild("Settings") then --<< It is a interactable object
			if (player.Character.HumanoidRootPart.Position-mouse.target.Position).magnitude <= mouse.target.Parent.Settings.interact.distance.Value then --<< in range
				--<< In range, enable tooltip
				player.PlayerGui:WaitForChild("Core").Cursor.Text = mouse.target.Parent.Settings.interact.tooltip.Value
			end
		else
			player.PlayerGui:WaitForChild("Core").Cursor.Text = ""
		end
	else
		player.PlayerGui:WaitForChild("Core").Cursor.Text = ""
	end
end)


player.Character.Humanoid.Running:Connect(function()
	if mouse.target then
		if mouse.target.Parent:FindFirstChild("Settings") then --<< It is a interactable object
			if (player.Character.HumanoidRootPart.Position-mouse.target.Position).magnitude <= mouse.target.Parent.Settings.interact.distance.Value then --<< in range
				player.PlayerGui:WaitForChild("Core").Cursor.Text = mouse.target.Parent.Settings.interact.tooltip.Value
			end
		else
			player.PlayerGui:WaitForChild("Core").Cursor.Text = ""
		end
	else
		player.PlayerGui:WaitForChild("Core").Cursor.Text = ""
	end
end)
1 Like

Here is where the error is.

if (player.Character.HumanoidRootPart.Position-mouse.target.Position).magnitude <= mouse.target.Parent.Settings.interact.distance.Value then --<< in range
	--<< In range, enable tooltip
	player.PlayerGui:WaitForChild("Core").Cursor.Text = mouse.target.Parent.Settings.interact.tooltip.Value
end
-- Do nothing if out range, don't update the cursor text.

I’d clean it up and write it like this, but you could be fix the old code if you really prefer that.

local cursor = player.PlayerGui:WaitForChild("Core").Cursor
local root = player.Character.HumanoidRootPart

local function updateTooltip()
	
	local text = "" -- Default to blank
	local target = mouse.target

	if target then
		local settings = target.Parent:FindFirstChild("Settings")
		if settings and (root.Position - target.Position).magnitude <= settings.interact.distance.Value then
			text = settings.interact.tooltip.Value -- Passes all the checks, fill in the tooltip
		end
	end

	cursor.Text = text -- Blank, or the tooltip
		
end

mouse.Move:Connect(updateTooltip)
player.Character.Humanoid.Running:Connect(updateTooltip)
2 Likes

Humanoid.Running is a value that changes everytime you stop/start walking. Consider making a while wait() do inside your Humanoid.Running function and from inside of the while loop you can check your if statements and everything will work!

you could always run from RunService.RenderStepped?

Use RenderStepped and to get distance from specific parts do
plr:GetDistanceFromCharacter(position)
It returns a number value of the distance. Hope this helped.

Thanks! This works perfectly, and the new formatting is much better. I’ll try to work on my formatting in future works :smiley: