Tweening/Lerping beam sequence properties

I’m trying to make a Beam Manager module script for my lasers in a core/reactor game, and I’m having major issues with tweening/lerping the transparency and colour. I’ve looked everywhere but couldn’t find any help. Here is my current code:

local Transparency = function(laser,transparency,style,styleTime)
	if typeof(laser) == "string" then
		laser = beamsFolder[laser]
	end
	--[[
	if transparency:isA("Number") then
		transparency = NumberSequence.new{
			NumberSequenceKeypoint.new(0, transparency)
		}
	end
	]]--
	if style == "Tween" then
		local beamTransparency = laser.Beam.Transparency.Keypoints[1].Value
		for i = 0, 1, wait() / styleTime do
			laser.Beam.Transparency = NumberSequence.new(beamTransparency:lerp(transparency, i))
			wait()
		end
	else
		laser.Beam.Transparency = NumberSequence.new(transparency)
	end
end

local Color = function(laser,color,style,styleTime)
	if typeof(laser) == "string" then
		laser = beamsFolder[laser]
	end
	if style == "Tween" then
		local beamColor = beamsFolder[laser].Beam.Color.Keypoints[1].Value
		for i = 0, 1, wait() / styleTime do
			beamsFolder[laser].Beam.Color = ColorSequence.new(beamColor:lerp(color, i))
			wait()
		end
	else
		beamsFolder[laser].Beam.Color = ColorSequence.new(color)
	end
end

And here is the error:

Help is majorly appreciated.

Try using a NumberValue/Color3Value and then on .Changed, update the property. Colour sequences and number sequences don’t support tweening or lerping unfortunately.

It’s a wonky workaround but it should work.

2 Likes

I have 8 lasers to work with, and then I have multiple more decorational ones that I have to work with for each of these 8 lasers. I’ll see what I can do though.

Gotcha.

I’ll try and help you out here. Something like this should work:

local transparency = function(laser, transparency, style, styleTime)
    if type(laser) == 'string' then -- try using type if you aren't specifically looking for roblox datatypes as it's quite a bit faster. This is micro-optimization though and doesn't make a huge difference. You could also probably do laser = beamsFolder[laser] or laser
        laser = beansFolder[laser]
    end
    if style == 'Tween' then
        local transparencyValue = Instance.new('NumberValue')
        local beamTransparency = laser.Beam.Transparency.Keypoints[1].Value
        transparencyValue.Value = beamTransparency
        tweenService:Create(transparencyValue, TweenInfo.new(styleTime, Enum.EasingStyle.Linear), Value = transparency):Play()
        transparencyValue.Changed:Connect(function()
            laser.Beam.Transparency = NumberSequence.new(transparencyValue.Value)
        end)
    end
end

Doing this with ColorSequences would essentially be the same, except instead of using a NumberValue, you’d use a Color3Value.

1 Like


Stuck with this error.

	if style == "Tween" then
		local transparencyValue = laser.Transparency
		local beamTransparency = laser.Beam.Transparency.Keypoints[1].Value
		transparencyValue.Value = beamTransparency
		TS:Create(transparencyValue, TweenInfo.new(styleTime, Enum.EasingStyle.Linear), {Value = transparency}):Play()
		transparencyValue.Changed:Connect(function()
			laser.Beam.Transparency = NumberSequence.new(transparencyValue.Value)
		end)

Clearly configured properly.

It’s going to return the property if you’re indexing it like that.

Try this:

local transparencyValue = laser:FindFirstChild('Transparency')
1 Like

Still getting the same error apparently.

image

local Transparency = function(laser,transparency,style,styleTime)
	if type(laser) == 'string' then
		print(laser)
		laser = beamsFolder[laser]
	end
	--[[
	if transparency:isA("Number") then
		transparency = NumberSequence.new{
			NumberSequenceKeypoint.new(0, transparency)
		}
	end
	]]--
	if style == "Tween" then
		local transparencyValue = laser:FindFirstChild("Transparency")
		print(transparencyValue)
		local beamTransparency = laser.Beam.Transparency.Keypoints[1].Value
		transparencyValue.Value = beamTransparency
		TS:Create(transparencyValue, TweenInfo.new(styleTime, Enum.EasingStyle.Linear), {Value = transparency}):Play()
		transparencyValue.Changed:Connect(function()
			laser.Beam.Transparency = NumberSequence.new(transparencyValue.Value)
		end)
	else
		laser.Beam.Transparency = NumberSequence.new(transparency)
	end
end

Wait I need to find the descendant of the BEAM, not the PART.

1 Like

Try this, if no Transparency numbervalue exists in the laser, it will create a new one.

    if style == "Tween" then
		local transparencyValue = laser:FindFirstChild('Transparency') or Instance.new('NumberValue')
        transparencyValue.Parent = laser
        transparencyValue.Name = 'Transparency'
		print(transparencyValue)
		local beamTransparency = laser.Beam.Transparency.Keypoints[1].Value
		transparencyValue.Value = beamTransparency
		TS:Create(transparencyValue, TweenInfo.new(styleTime, Enum.EasingStyle.Linear), {Value = transparency}):Play()
		transparencyValue.Changed:Connect(function()
			laser.Beam.Transparency = NumberSequence.new(transparencyValue.Value)
		end)
	else
		laser.Beam.Transparency = NumberSequence.new(transparency)
	end

I was right, but now:


I’ll need to change all the lines that fire the setProperty function in the module.

Oh gotcha. Are you passing a NumberSequence value to the function? It should be a number. Pretty sure that’s the only time the transparency variable gets assigned.

1 Like

Almost done, but when it is fired a second time, this occurs repeatedly:

Hmm.

Try:

laser.Beam.Transparency = NumberSequence.new({NumberSequenceKeypoint.new(0, transparencyValue.Value); NumberSequenceKeypoint.new(1, transparencyValue.Value)})
1 Like

Thanks! It works perfectly fine now, and I can now configure so much more!

1 Like