Tween not playing as intended

I have a function that runs periodically where essentially while tweening in, it will need to appear before the tween plays and conversely while tweening out, it will disappear after the tween plays.

The issue is that when it’s supposed to tween out, b.Transparency = 1 is running before the tween. Essentially, the object disappears before the tween plays

Aside from task.wait() i have also tried using wait() and tween.Completed:Wait() but the same thing happens

local function tweenArrow(obj, var, ti)
	local tween = tweenService:Create(obj, ti, {Position = (obj.Parent:WaitForChild("origPosition").Value + Vector3.new(0, var, 0)) + (obj.Position - obj.Parent:WaitForChild("PrimaryPart").Position)})
	tween:Play()
	-- i don't think this is relevant
end

local function toggleInteractionArrow(obj, var, ti)
	for _, a in pairs(obj:GetChildren()) do
		if var == 0 and obj.Name ~= "PrimaryPart" then
			a.Transparency = 0
		end
		task.spawn(tweenArrow, a, var, ti) --where the model/object tweens
	end
	if var ~= 0 then
		task.wait(1)
		for _, b in pairs(obj:GetChildren()) do
			b.Transparency = 1
		end
	end
end

so if var is 0 then it’s tweening in which you can see with the a.Transparency = 0 the object appears before tweening
otherwise it’s tweening out but then again the object disappears before the tween

I found that the problem was with a different section of the script running the function repeatedly. I just added a debounce system to prevent it.

local function tweenArrow(obj, var, ti)
	local tween = tweenService:Create(obj, ti, {Position = (obj.Parent:WaitForChild("origPosition").Value + Vector3.new(0, var, 0)) + (obj.Position - obj.Parent:WaitForChild("PrimaryPart").Position)})
	tween:Play()
	if var == 0 then
		if obj.Name ~= "PrimaryPart" then
			obj.Transparency = 0
		end
	else
		tween.Completed:Wait()
		obj.Transparency = 1
	end
end

local function toggleInteractionArrow(obj, var, ti)
	if not obj:FindFirstChild("isTweening").Value then
		obj.isTweening.Value = true
		for _, a in pairs(obj:GetChildren()) do
			if a.Name ~= "origPosition" and a.Name ~= "isTweening" then
				task.spawn(tweenArrow, a, var, ti)
			end
		end
		task.wait(1)
		obj.isTweening.Value = false
	end
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.