Guys i'm trying to loop this script when it go to 1 Transparency it back to 0 Transparency

Hello. What seems to be the error?
EDIT: I do not see the need for using a while true do loop in this context. You should remove that.

1 Like

Your loop won’t work cause you encased the Touched Event inside it, so it’s waiting for the part to be touched

ALSO YOUR PC IS DEAD FIX IT BY ADDING A WAIT()

3 Likes

Why are you trying to loop a function? What are you trying to do?

2 Likes

If you’re looping it why even make it a function?

You should also use renderstepped while do loops won’t end very well, especially without a wait() in it

1 Like

No dont add wait() wait is bad i hate wait() it is bad wait() is slow

1 Like

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

The script is basically

while true do
    print("XDDDDDDDDDDDD")
end

There’s no delay inbetween the loop

Even if you see waits(), that’s cause they’re encased inside the function waiting to be fired from

1 Like

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!

1 Like

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

1 Like

I hate while do loops and wait both are bad

We have RunService.RenderStepped for this specific reason. It is funny how all of my scripts from 1 year ago are full of while true do wait() end

1 Like

So what do I do? :thinking: :thinking: :thinking:

1 Like

What are you even trying to do in the first place?

You should probably understand why wait() is generally discouraged instead of spreading propoganda

1 Like

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
script.Parent.Touched:Connect(onTouched)

local poop = false

function onTouched()
   if poop then return end
   poop = true
   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)
   poop = false
end

script.Parent.Touched:Connect(onTouched)
1 Like

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

As said above:

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

3 Likes

Maybe try:

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!

1 Like

Why did you just use wait()? Did you just see @QuantixDev’s post?

1 Like