I just started scripting not even a day ago and I need a little help with efficiency. I couldn’t fit the whole code in but all I would like to know is if there is a faster way to call several functions, so when I’m making this vanishing stair case I don’t have to write out a new function every single time. Any help would be much appreciated.
-
You can copy your code, paste it into your forum post, and surround it with three backticks
```
like this
```and it will render
like this
-
Yep! Welcome to the world of functions.
As a first step, you can factor out the duplicated code into one function and just call it three times:
local step1 = game.Workspace.step1 local step2 = game.Workspace.step2 local step3 = game.Worksapce.step3 local function DoSomething(part) -- we can have the function take in an argument wait(0.1) part.CanCollide = false -- and we use "part" instead of using a variable like "step1" direcly part.Transparency = 1 wait(1) part.CanCollide = true part.Transparency = 0 end DoSomething(step1) DoSomething(step2) DoSomething(step3)
Thanks a lot, on line 5 for DoSomething, if I was making it so when you touch it it activates would I do Touch:Connect?
Yes, but there’s some nuance.
step1.Touched:Connect(DoSomething)
^ That wouldn’t work how you want, since Touched
would call DoSomething
with the part that touched step1
. not step1
itself.
One way to do it would be to have Touched
call a anonymous function whose only purpose is to call DoSomething
with the right argument:
-- note that you don't actually need to include "partThatTouchedStep1", I
-- just included it for example's sake
step1.Touched:Connect(function(partThatTouchedStep1)
DoSomething(step1)
end)
Kind of confused
local step1 = game.Workspace.step1
local step2 = game.Workspace.step2 --creates the variable
local step3 = game.Workspace.step3
local function Touch:Connect(step1,step2,step3)
Touch:Connect(step1)
Touch:Connect(step2)
Touch:Connect(step3)
wait(0.1)
part.CanCollide = false
part.Transparency = 1
wait(1)
part.CanCollide = true
part.Transparency = 0
end
step1.Touched:Connect(walk1)
step2.Touched:Connect(walk2) -- calls the function
step3.Touched:Connect(walk3)
Not quite.
local step1 = game.Workspace.step1
local step2 = game.Workspace.step2
local step3 = game.Worksapce.step3
local function DoSomething(part) -- we can have the function take in an argument
wait(0.1)
part.CanCollide = false -- and we use "part" instead of using a variable like "step1" direcly
part.Transparency = 1
wait(1)
part.CanCollide = true
part.Transparency = 0
end
step1.Touched:Connect(function() DoSomething(step1) end)
step1.Touched:Connect(function() DoSomething(step2) end)
step1.Touched:Connect(function() DoSomething(step3) end)
Or, if you want to be extra lazy, you could connect the Touched
event in a separate function as well.
Hopefully my super long argument names help you understand what’s going on :
local step1 = game.Workspace.step1
local step2 = game.Workspace.step2
local step3 = game.Worksapce.step3
local function DoSomething(partThatWillDoSomething)
wait(0.1)
partThatWillDoSomething.CanCollide = false
partThatWillDoSomething.Transparency = 1
wait(1)
partThatWillDoSomething.CanCollide = true
partThatWillDoSomething.Transparency = 0
end
local function SetupTouchedToDoSomethingFor(partThatWillBeTouched)
partThatWillBeTouched.Touched:Connect(function()
DoSomething(partThatWillBeTouched)
end)
end
SetupTouchedToDoSomethingFor(step1)
SetupTouchedToDoSomethingFor(step2)
SetupTouchedToDoSomethingFor(step3)