while true do
function onTouched()
script.Parent.CanCollide = false
script.Parent.Transparency = 0
wait(1)
script.Parent.Transparency = 0.50
wait(1)
script.Parent.Transparency = 1
script.Parent.CanCollide = true
wait(3)
end
end
He only mentioned wait() in this case because in @OPs original code, there was no yield that can be easily reached, meaning if they were to ever run the code, the script would be exhausted and could crash their studio
An empty wait() function does not slow down your code. In the contrary, it actually helps in keeping them under the server’s limit, thus speeding the performance up!
Actually wait() is considered code smell and can cause noticeable slow downs in things that actively use it. If there’s a reason not to use wait(), go for it. Like this case, @OP doesn’t need a loop for this
Should be noted though embat that when u do that you are adding code for the computer to tell the scripts to do, acting as a proxy in a sense. if its not critical to the game its generally better to use a more serial approach than using run service
You can learn more about how wait impacts performance of scripts, and alternate methods here.
Wait can be replaced with numerous methods, the most noteable one is RunService. The reason behind this is because RunService runs on the 60hz task scheduler rather than the 30hz task scheulder (Which wait() runs on), RunService also provides you with a delta value which lets you account values for delay between event fires.
Example of using RunService (to change transparency)
local RunService = game:GetService("RunService")
-- This will fire 60 times a frame
-- The 'delta' argument is the time between each RenderStepped event
local increment = 0
RunService.RenderStepped:Connect(function(delta)
-- this will adjust the variable 'increment' by 1 every second
increment += 1 * delta
part.CFrame *= CFrame.Angles(0, math.rad(increment), 0) -- this will rotate a part 1 degree every second on the y axis.
end)
Another example, showing the use of RunService:Wait()
local RunService = game:GetService("RunService")
-- 1/60 means this for loop will last 1 second
-- You can calculate this value by doing (1 / how long you want the loop to last)
-- E.g 2 seconds would be 1/2 = 0.5 (0.5/60)
for i=0, 1, 1/60 do
part.Transparency = i -- smoothly set the transparency of a part over 1 second
RunService.RenderStepped:Wait() -- waiting
end
Hope these examples help you understand the benefits of RunService
function onTouched()
while true do
script.Parent.CanCollide = false
script.Parent.Transparency = 0
wait(1)
script.Parent.Transparency = 0.50
wait(1)
script.Parent.Transparency = 1
script.Parent.CanCollide = true
wait(3)
end
wait()
end
script.Parent.Touched:Connect(onTouched)
you should look at the other comments too by the way, they ~maybe~ ARE helpful, this is a simple script, just use it if you want to not use runservice! BUT IT IS RECOMMENDED BY DEVELOPERS HERE! so refer to them!