Hey everyone,
so this is my first topic ever and i hope you are not yelling at me for asking this.
So im doing a Stage Play and i use Beams to mimic Theater Lights.
Now im comming to the Point where i have to tweak the Transparancy and i do it like this
to reach a certain point and make them turn on and off smoothly.
This can get quite lengthy and anoying to do if you have numbers like (0.2,0.5)
Is there an easier way to make this less Type intensive?
Its my first year of Coding and i got quite far with Tutorials and the DevForum but im reaching this point where i need to ask for advise. I hope this is not a Dumb question. Thanks for your help everybody and stay healthy =)
Hello, and welcome!
Donât worry about asking basic questions. We all were there at some point.
There IS a much easier way of doing this. It is called the TweenService. This Service tweens an objectâs properties in a given amount of time.
I could go quite deep into explaining how it works, but you can read about it or watch YouTube tutorials.
local TweenService = game:GetService("TweenService")
local ClickDetector = script.Parent.Parent.Parent.ClickDetector
-- Beams
local S = workspace.B1.Bulb.LightPart.Beam
local tween1 = TweenService:Create(S, TweenInfo.new(10), {Transparency = 0})
ClickDetector.MouseClick:Connect(function()
tween1:Play()
tween1.Completed:Wait() -- Will yield until the tween finishes
-- Now you can do whatever else you need!
end)
clickdetector.MouseClick:Connect(function()
S.Enabled = true
for i = 100, 0, -1 do --Does this 100 times
S.Transparency = NumberSequence.new(i/100,1) --Divides the current iteration by 100
wait(0.1)
end
end)
You just need to do some simple tweaks to also include the 2nd argument of a NumberSequence, itâs way simpler than what you were trying to do
Okay my brain completely forgot you could also subtract by decimals instead haha
Hey Dash8Q4, donât worry about us yelling at you, weâre here to help! This is exactly how I coded when I was just getting started, too!
I think what youâre looking for is a loop, and more specifically a âfor loop.â Itâs a block of code that can repeat itself, saving time and shortening your script.
(Edit: I agree with Salinas23 that TweenService is a very useful way to accomplish what youâre trying to do, but I think you might also benefit from what I have typed out here as it contains some basic programming that will come in handy in all sorts of different situations )
It looks like you want the first number in your number sequence to reduce by 0.01 every 1/10 of a second. You would be able to accomplish that with this code:
for i = 1, 0, -0.01 do
S.Transparency = NumberSequence.new(i,1)
wait(0.1)
end
What this means is that weâre declaring a new variable called âiâ (which we set to equal 1 at the beginning) and iterating over this code block until i reaches 0.0, and decreasing it by 0.01 every time.
In the block of code, weâre using that variable âiâ as the first number and waiting 0.1 seconds until the next iteration.
I use TweenService for my SpotLights and Models to Move already but when i tried it with the NumberSequenz on my Beam it said that it cant be done.
So when i click a button and the script fires, it does only go to the Set i number and not loops continuesly?
I added a screenshot of the Stage i build and the Beams so you have a better picture what i mean. Sorry for adding that so late.
I will try to play arround with the codes and see how they work. Stuff like this is still a bit complicated to me because i never worked with "for i " statements and im a bit scared to use them.
EDIT: i forgot to add that i sometimes need to tweak both numbers so the END and START point of the Beam, will i be able to do this with two numbers at the same time?
TweenService:Create property named âTransparencyâ cannot be tweened due to type mismatch (property is a âNumberSequenceâ, but given type is âdoubleâ) - Server - Script:6
In that table in the 3rd argument in TweenService? Itâs erroring cause it expected a NumberSequence but a number was given. Of course you could change the 0s to whatever number you need to make it work
Then youâd have to use a for loop for that then if it canât be tweened, you could use what I, or better, @vanilla_wizard had mentioned.
As for looping 2 things at once, youâd have to use coroutines for that, I canât really explain that in deal as itâs very late for me. Iâd recommend reading the beginnerâs guide to coroutines to help explain things for you. If you only have one for loop, a coroutine is not needed, but if you need 2 to run together, you need to use a coroutines
ok cool. I will look into that. I love tweens, super easy to use actually once you got the hang of it. Im using the loop code now to play arround with and learn how it works. Thank you so much for your time already, its very much appriciated =)