Script produces strange behavior

I needed to make a part that grows in size and becomes transparent at the same time. This works most of the time, but at seemingly random times it doesn’t.
Here’s the code:

local ts = game:GetService("TweenService")

local smallsize = Vector3.new(30.466, 35.83, 34.012)
local bigsize = Vector3.new(49.166, 58.116, 55.083)

local radiate = false

workspace.CoreData.Radiate.Changed:Connect(function(val)
	radiate = val
	if val == true then
		script.Parent.Transparency = 0
	else
		script.Parent.Transparency = 1
		script.Parent.Size = smallsize
	end
end)

while wait() do
	if radiate == true then
		script.Parent.Transparency = 0
		ts:Create(script.Parent,TweenInfo.new(2,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut),{Size = bigsize, Transparency = 1}):Play()
		wait(2)
		script.Parent.Size = smallsize
	end
end

And here’s a video showing what happens. The issues begin around the 30-second mark.

Does anybody see any issues with the code that might cause this strange behavior?

So I would take a guess, that

wait(2)
script.Parent.Size = smallsize

in the while loop is your problem. I think that sometimes the Size is changed before the Tween is completed and the tween overwrites it. Try it with the .Completed Event.

while wait() do
	if radiate == true then
		script.Parent.Transparency = 0
		ts:Create(script.Parent,TweenInfo.new(2,Enum.EasingStyle.Sine,Enum.EasingDirection.InOut),{Size = bigsize, Transparency = 1}):Play()
		ts.Completed:Wait()
        task.wait(0.3) --EXTRA DEBOUNCE. If that works then test it without that and if it works, delete it.
		script.Parent.Size = smallsize
        print("Changing to: "..smallsize)
	end
end

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