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)
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.
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.
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.