Why is my coroutine always returning suspended status?

I’m making an interactive GUI for multiple tagged items. For some reason, the status of my coroutine always returns the suspended status. It doesn’t yield either, the second resume doesn’t work and only works for the first tagged item.

local Interaction = coroutine.create(function(Frame, KeyText, ActionText, Action, Key, TaggedParts)
	RS.Heartbeat:Connect(function()
		Frame.Visible = false
		for _,item in pairs(TaggedParts) do
			if (item.PrimaryPart.Position - humanoid.Position).magnitude <= 6 and mouse.Target == item.PrimaryPart then	
				Frame.Visible = true
				ActionText.Text = Action
				KeyText.Text = Key
			end
		end
		coroutine.yield()
	end)
end)

coroutine.resume(Interaction, AboveHoverFrame, AboveKey, AboveAction, "Open", "E", Doors)
coroutine.resume(Interaction, BelowHoverFrame, BelowKey, BelowAction, "Dispose", "LMB", Disposables)

It’s because your thread context changed. The function connected to Heartbeat is running in a different coroutine.

1 Like

Would I need to change the Heartbeat loop to something like a while loop or a for i = loop?

I’m not really sure what the point of your coroutine thing is here. If all you’re trying to do is toggle visibility of the UI, you could remove the coroutine stuff and just use the heartbeat loop.

local function Interaction(Frame, KeyText, ActionText, Action, Key, TaggedParts)
	RS.Heartbeat:Connect(function()
		Frame.Visible = false
		for _,item in ipairs(TaggedParts) do
			if (item.PrimaryPart.Position - humanoid.Position).Magnitude <= 6 and mouse.Target == item.PrimaryPart then	
				Frame.Visible = true
				ActionText.Text = Action
				KeyText.Text = Key
				break
			end
		end
	end)
end

Interaction(AboveHoverFrame, AboveKey, AboveAction, "Open", "E", Doors)
Interaction(BelowHoverFrame, BelowKey, BelowAction, "Dispose", "LMB", Disposables)
2 Likes

Yes, I changed it to that earlier, realizing that I didn’t need a coroutine. Thank you for the tip, though :smile:

I know this is an old post, but thank you so much. I had no idea about thread context until now, you solved so many problems for me.