4 Questions regarding a piece of code that could be improved


#1

Hey!
So long story short, I’m pretty new into scripting and I’m still learning a lot.
So what I was trying to do is when you touch this part, it fades away from transparency 0 to 1 then another model fades up from transparency 1 to 0

Here is the code:

round = game.workspace.Circle

game.Workspace.Circle.touched:connect(function()
round.Transparency = 0.1
wait(0.1)
round.Transparency = 0.2
wait(0.1)
round.Transparency = 0.3
wait(0.1)
round.Transparency = 0.4
wait(0.1)
round.Transparency = 0.5
wait(0.1)
round.Transparency = 0.6
wait(0.1)
round.Transparency = 0.7
wait(0.1)
round.Transparency = 0.8
wait(0.1)
round.Transparency = 0.9
wait(0.1)
round.Transparency = 1
wait(3)
for i,v in pairs(game:GetService(“Workspace”):FindFirstChild(“Model”):GetChildren()) do
wait(0.1)
v.CanCollide = true
v.Transparency = 0.9
wait(0.1)
v.Transparency = 0.8
wait(0.1)
v.Transparency = 0.7
wait(0.1)
v.Transparency = 0.6
wait(0.1)
v.Transparency = 0.5
wait(0.1)
v.Transparency = 0.4
wait(0.1)
v.Transparency = 0.3
wait(0.1)
v.Transparency = 0.2
wait(0.1)
v.Transparency = 0.1
wait(0.1)
v.Transparency = 0
wait(0.1)
end
end)

So what happens when I run the script is:

Now I have a few Questions regarding how I can make this better

Question 1:
How can I make these parts load faster, it takes around 1-2 seconds for each part to load which is annoying, I need the entire model to load up in less then a second.

Question 2: how do I make the Entire model fade in at once instead of having each individual part loading in until the entire model is visible.

Question 3: How do I make a debounce script for this?

Question 4: is there a way to make this Transparency Fade thing easier instead of having to type in every transparency number until it reaches 0?

Thank you for your time, you don’t have to answer all the question. I’d truly appreciate it if you can answer those you know about!


#2

#1: Decrease the time used in the wait() calls (e.g. make it 0.05 instead of 0.1).

#2: You’ll need to loop through the children of the model and use coroutines or spawn() on the fade function to get them running synchronously.

For example:

local function Fade(Part)
    -- fade code
end

for _, Child in pairs(Model:GetChildren()) do -- loop through children of Model
    coroutine.wrap(Fade)(Child) -- coroutine.wrap() let's you make a function run on a different 'thread'
end

#3: To make a debounce, you do something like this:

local Debounce = false

Part.Touched:Connect(function()
    if not Debounce then -- if debounce isnt running
        Debounce = true -- enable debounce
        -- code
        wait(1)
        Debounce = false -- disable debounce
    end
end)

#4: Yes, you can use a loop or TweenService, for example:

for Transparency = 1, 0, -0.1 do -- go from 1 to 0, taking away 0.1 every time
    wait(0.1)
    Part.Transparency = Transparency -- set the part transparency to the current number
end

-- OR

local Info = TweenInfo.new(0.1) -- the time it takes to tween, among other things
local Properties = {Transparency = 0} -- the properties you want to tween
local Tween = game:GetService("TweenService"):Create(Part, Info, Properties)
Tween:Play() -- play the tween

Since you’re a new scripter, I’m probably introducing lots of new concepts, so it would do you good to look them up on the wiki or ask people about them :wink:


#3

Thanks man, Just another question (kinda off topic)
you know how we use game.Part.Touched to check if someone has touched a part, what if I want to check if someone has clicked a textbutton inside a screenGUI?


#4
TextButton.MouseButton1Down:Connect(function()
   print('Hello')
end)

Note

For Guis & UserInput always use a LocalScript because a script won’t work if FE(Filtering Enabled) is on


#5

You can use two for-loops to do this easily. No need for multiple threads spawned.

local model = workspace.Model
for i = 1,10 do
	for _,v in pairs(model:GetChildren()) do
		v.Transparency = v.Transparency + 0.1
	end
	wait(0.1)
end