My function here runs on a RenderStepped event. The problem lies towards the bottom of the script. When the player leaves the vicinity of an interact-able item, I want to have the interact button tween down in size then be destroyed, however it just gets destroyed immediately. When doing prints I get
Playing tween x2
Tween complete
So Playing tween prints twice, while tween complete prints once. These happen at the exact same time. After 2 seconds, I get a second Tween complete print
local function Render()
local Character = Player.Character
if not Character then return end
local LastInput = UserInputService:GetLastInputType()
local ClosestDistance = math.huge
-- Check for closest interaction
for _, v in pairs(AllInteractables) do
local Model = v.Adornee
if Player:DistanceFromCharacter(Model.PrimaryPart.Position) <= ClosestDistance then
-- Set values
ClosestAction = Model
ClosestDistance = Player:DistanceFromCharacter(Model.PrimaryPart.Position)
end
end
-- Confirmation of ClosestAction
for _, v in pairs(AllInteractables) do
if v.Adornee == ClosestAction then
-- ClosestAction, E to interact
if LastInput == Enum.UserInputType.Touch then
-- On mobile, can only show Tap
v.Control.Key.Visible = false
v.Control.Tap.Visible = true
else
-- On PC, can show E
v.Control.Key.Visible = true
v.Control.Tap.Visible = false
end
else
-- Other action, tap to interact
v.Control.Key.Visible = false
v.Control.Tap.Visible = true
end
end
-- Check for tags
for _, v in pairs(CollectionService:GetTagged("Interaction")) do
if CollectionService:HasTag(v, "Player") then
if v == Character then return end -- Player can't interact with themselves
end
if not v.PrimaryPart and not v then return end -- Just make sure the PrimaryPart exists
if Player:DistanceFromCharacter(v.PrimaryPart.Position) <= ReachDistance then
-- Check for previous interact
local GuiFound = false
for _, gui in pairs(Interactables:GetChildren()) do
if gui.Adornee == v then
GuiFound = true -- Don't create a second gui
end
end
if not GuiFound then
local NewInteractGui = Interact:Clone()
NewInteractGui.Adornee = v
NewInteractGui.Name = v.Name
NewInteractGui.Parent = Interactables
-- Make the button grow in size
local Tween = TweenService:Create(NewInteractGui.Control, TweenInfo.new(0.15), {Size = UDim2.new(1, 0, 1, 0)})
Tween:Play()
--// EDIT Ask nuke if this will cause memory leak
NewInteractGui.Control.Activated:Connect(function()
StartInteract(v)
end)
end
else
for _, gui in pairs(Interactables:GetChildren()) do
if gui.Adornee == v then
-- Previous interact found, destroy it
local Tween = TweenService:Create(gui.Control, TweenInfo.new(2), {Size = UDim2.new(0, 0, 0, 0)})
Tween:Play()
print("Playing tween")
Tween.Completed:Wait() -- Wait for tween to complete
print("Tween complete")
gui:Destroy()
end
end
end
end
end