Multiple interaction guis not being actually destroyed when left vacinity

I’m using CollectionService to tag multiple items with the ‘Interaction’ tag. I then go through all them and see if the player is near them, and thus create a gui, otherwise delete the gui when they leave the area.

local function Render()
	local Character = Player.Character
	if not Character then return end
	-- Check for tags
	for _, v in pairs(CollectionService:GetTagged('Interaction')) do
		if CollectionService:HasTag(v, 'Player') then
			if v == Player then return end -- Player can't interact with themselves
			v = v.Character -- Set v == the players character (so v.PrimaryPart below works)
		if Player:DistanceFromCharacter(v.PrimaryPart.Position) <= ReachDistance then
			-- Check for previous interact
			for _, gui in pairs(Interactables:GetChildren()) do
				if gui.Adornee == v then return end -- Don't create a second gui
			local NewInteractGui = Interact:Clone()
			NewInteractGui.Adornee = v
			NewInteractGui.Name = v.Name
			NewInteractGui.Parent = Interactables
			ClosestAction = v
			for _, gui in pairs(Interactables:GetChildren()) do
				if gui.Adornee == v then
					-- Previous interact found, destroy it


However I get problems where it doesn’t actually delete the gui when it loads another in the same spot.

robloxapp-20200817-1241291.wmv (2.6 MB)

Can see multiple problems in the vid. For one, one of the interactables dont get picked up, even when im standing on top of it, and doesnt work till i move away from the other 2.
Another problem is the gui’s not acting independently, so 2 stay on the screen, even tho I’m further than 20 studs from it.

Well, I noticed that you stop the execution of the whole function with return if the interaction object is the player and also, if the adornee of a interactable gui is v. Doesn’t this mean that it won’t always check every object? Or did I misunderstand something?

1 Like