Tween Problem Maybe?

Hey, thanks for coming to my Ted Talk!

Basically I have an “e to interact” thing and it works perfectly, but i’ll try to explain this as best I can.

When there are more than 1 objects that are “Interactable” the gui constantly tweens In and out.
Whenever I have just one (as shown in the video) it does it’s intended behavior which is stay in place.

My Code: (Note Isn’t all of it, just where the problem comes form, please note it’s running on renderstepped so (???)

for i,v in pairs(workspace.Interactables:GetChildren()) do
		if v:IsA("BasePart") then
			if (v.Position - char.HumanoidRootPart.Position).magnitude < maxMag then
				if plr.PlayerGui.contextInteract.Frame.Position ~= UDim2.new(0.5, 0, 0.9, 0) then
					plr.PlayerGui.contextInteract.Frame.TextLabel.Text = keyToPress.." to interact with "..v.Name
					plr.PlayerGui.contextInteract.Frame:TweenPosition(UDim2.new(0.5, 0, 0.9, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quint, .5, false)
				end
			else
				if plr.PlayerGui.contextInteract.Frame.Position == UDim2.new(0.5, 0, 0.9, 0) then
					plr.PlayerGui.contextInteract.Frame:TweenPosition(UDim2.new(0.5, 0, 1.2, 0), Enum.EasingDirection.In, Enum.EasingStyle.Quint, .5, false)
				end
			end
		end
	end

You need to make a variable at the start before the loop of closestObject which you set to any object that is closer than your threshold and closer than any existing object found in a previous iteration.

If there is no closest object, tween out, otherwise tween in.

local closestObject = nil
local closestMag = 0

for i,v in pairs(workspace.Interactables:GetChildren()) do
	if v:IsA("BasePart") then
        local mag = (v.Position - char.HumanoidRootPart.Position).magnitude
		if mag < maxMag and not closestObject or mag < closestMag then
			closestMag = mag
            closestObject = v
		end
	end
end

if closestObject and plr.PlayerGui.contextInteract.Frame.Position ~= UDim2.new(0.5, 0, 0.9, 0) then
    plr.PlayerGui.contextInteract.Frame.TextLabel.Text = keyToPress.." to interact with "..closestObject.Name
    plr.PlayerGui.contextInteract.Frame:TweenPosition(UDim2.new(0.5, 0, 0.9, 0), Enum.EasingDirection.Out, Enum.EasingStyle.Quint, .5, false)
elseif not closestObject and plr.PlayerGui.contextInteract.Frame.Position == UDim2.new(0.5, 0, 0.9, 0) then
    plr.PlayerGui.contextInteract.Frame:TweenPosition(UDim2.new(0.5, 0, 1.2, 0), Enum.EasingDirection.In, Enum.EasingStyle.Quint, .5, false)
end
1 Like

I have that already whenever I click “e” even if they aren’t near each other no matter what they both ALWAYS tween 2x until there is one interactable left to interact when, then it decides to stop constantly tweening

You don’t have that already on the tweening which is what I was referring to. I’ve included a code snippet on my first reply using your code to explain it better.

1 Like