GUI Interaction function not working with separate tags

Is it possible to make a function on one local script, then have it work for multiple separate tagged items in the workspace. This is an interaction gui, by the way.

Example :

function Interaction(Frame, Action, ActionFrame, KeyFrame, Key, TaggedItems)
while wait() do
		Frame.Visible = false
		for _,item in pairs(TaggedItems) do
			if (item.PrimaryPart.Position - game.Players.LocalPlayer.Character.PrimaryPart.Position).magnitude <= 6 and game.Players.LocalPlayer:GetMouse().Target == item.PrimaryPart then	
				Frame.Visible = true
				ActionFrame.Text = Action
				KeyFrame.Text = Key
               break
			end
		end
	end
end

Interaction() --Imagine I filled it out and everything
Interaction() --Now Imagine I filled it out with a different tag

This doesn’t work for some reason. I don’t want to write the same code and function block multiple times. Is there a less hacky way of doing this? Thank you. :smile:

2 Likes

It may be an issue to do with the fact that your function yields. When you call it for the first time it starts the loop and will keep repeating the loop until it breaks before calling the function for a second time.

You can use spawn() or coroutine.create to wrap the loop so this doesn’t happen

2 Likes

I looked into coroutines and coroutine.create and this works like a charm, thank you for your help. :smile: Have a great day!

1 Like