Working with Magnitudes

Hi, I’m working on a Magnitude script that uses .RenderStepped to display when the Player is near an interaction point.

However, the magnitude and all works perfectly fine, but once I add the function for the GUI to remove itself when out of distance, all of a sudden none of the other points are accessible as it doesn’t transfer to them.
Below is the code, is there something I’m missing?

RunService.RenderStepped:Connect(function()
for _,Panels in pairs (workspace:GetDescendants()) do
	if Panels.Name == "Panel" and Panels.Parent.Name == "Alarm" then
		
	if (Panels.Position - Character["HumanoidRootPart"].Position).magnitude < 10 then
	script:WaitForChild("Hotkey").Adornee = Panels
	script:WaitForChild("Hotkey").Enabled = true
	else 
	script:WaitForChild("Hotkey").Adornee = nil
	end
	
	else end
end
end)
2 Likes

Ideally this is the aim, with the ability for the GUI to be removed once at a distance:
https://i.gyazo.com/08559fe66d661ca18f70304a7da50d0d.mp4

And this is what happens when I attempt that:
https://i.gyazo.com/67dcb6961625c311da8eee64f8759a56.mp4

I see the issue,

the fact that this sets it to nil means that if the last one that is iterated through isn’t in range then it won’t show up at all.

Sidenote:

This will probably be a horrible option for performance, you should probably iterate through the children of a folder or use CollectionService instead.

1 Like

Hi, I tried simply setting the enabled to false instead of the Nil value, and it still persists.

And following your section option, how would I go about iterating through the Children decently?

Just don’t set it if its not in range, but if none are in range then set it to nil.

Also you can iterate through children with GetChildren()

So this could be changed to

for _,Panels in pairs(Alarms:GetChildren()) do -- or something similar, you may have to re do some things

I’m not quite following this,
this is what I’ve been attempting to do and is why I’m getting the issues I am. :confused:

If you remove this then you shouldn’t get the issue with it not appearing, but to make it so it resets when there are none in range just store some boolean to represent if any are in range.

Change your “else” statement to:

elseif script:WaitForChild("Hotkey").Adornee == Panels then
   script:WaitForChild("Hotkey").Adornee = nil
end

This will fix your issue, but overall, this code isn’t going to be very efficient.

1 Like
if (Panels.Position - Character["HumanoidRootPart"].Position).magnitude < 10 then
    -- code
end

I understand the problem has been solved but there is a nicer way to find the magnitude between the player and the panel using an inbuilt, faster function in C. The function is DistanceFromCharacter. Here would be an alternative way to calculate the magnitude:

if Player:DistanceFromCharacter(Panels.Position) < 10 then
    -- code
end

Note that the function is called on the Player instance, not the player’s character.

1 Like