Trying to realize the best way for magnitude interaction, help me

I currently have this simple script, but I am trying to understand if it always works like this, how/which parts get prioritized to get selected in the magnitude range?
I also use CollectionService to get all these parts.

local cs = game:GetService("CollectionService")
local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.E and not gameProcessed then
		for _,Interactable in pairs(cs:GetTagged("Interactable")) do
			local Magnitude = (Interactable.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude
			
			if Magnitude <= 10 then
				print(Interactable.Name)
			end
		end
	end	
end)

game:GetService("RunService").RenderStepped:Connect(function()
	script.Parent.InteractionPrompt.Visible = false
	
	for _,Interactable in pairs(cs:GetTagged("Interactable")) do
		local Magnitude = (Interactable.Position - game.Players.LocalPlayer.Character.HumanoidRootPart.Position).magnitude

		if Magnitude <= 10 then
			local coords2D = game.Workspace.CurrentCamera:WorldToScreenPoint(Interactable.Position)
			script.Parent.InteractionPrompt.Position = UDim2.new(0, coords2D.X, 0, coords2D.Y)
			script.Parent.InteractionPrompt.Visible = true
		end
	end
end)


Is this a good method cause I don’t know anything else lol.

My article might interest you!

Making a chunk system and only checking for objects tagged with "Interactable" in the surrounding chunks less often than the usual 60 times a second (if running on 60 fps) is better than all "Interactable"s 60 times a second.

3 Likes

This is a promising idea, I can’t believe I didn’t find your post when researching, it’s under-rated.

1 Like

But there is still a prioritization issue even w/ your method that I see :grimacing:
I believe there should be an easy fix, is it possible you can think of one?


Edit: I believe like you wrote, clearing the table should’ve fixed it but by even click E once 2 of them disappeared
Edit2: nvm it wasn’t to clear the last part you were near lol