Script doesn't work

I’m making a stage in a obby, where some parts fade in and out. I made a script where when the fading parts reach a certain transparency, the killing script turns off.
Note: The killing script is called “bruh”.

script:

local part = script.Parent

while true do
	part.Transparency = 0
	wait(0.1)
	part.Transparency = .1
	wait(0.1)
	part.Transparency = .2
	wait(0.1)
	part.Transparency = .3
	wait(0.1)
	part.Transparency = .4
	wait(0.1)
	part.Transparency = .5
	wait(0.1)
	part.Transparency = .6
	wait(0.1)
	part.Transparency = .7
	wait(0.1)
	part.Transparency = .8
	wait(0.1)
	part.Transparency = .9
	wait(0.1)
	part.Transparency = 1
	wait(0.1)
	part.Transparency = .9
	wait(0.1)
	part.Transparency = .8
	wait(0.1)
	part.Transparency = .7
	wait(0.1)
	part.Transparency = .6
	wait(0.1)
	part.Transparency = .5
	wait(0.1)
	part.Transparency = .4
	wait(0.1)
	part.Transparency = .3
	wait(0.1)
	part.Transparency = .2
	wait(0.1)
	part.Transparency = .1
	wait(0.1)
end

if part.Transparency == 1 or part.Transparency == .6 or part.Transparency == .7 or part.Transparency == .8 or part.Transparency == .9 then
	part. bruh.Enabled = false
else
	part. bruh.Enabled = true
end

if part.Transparency == 1 or part.Transparency <= .6 then
	part.CanCollide = false
end

Doesn’t work for some reason.

3 Likes

the problem here is that you’re checking the part’s transparency after it has been changed.

The solution would be to check the transparency while the loop is running using another script.

You can also use tweenservice to change your part’s transparency:

2 Likes

how would i do that?
(sorry if this is trival.)

2 Likes

The while loop is running infinitely, so any code after it won’t have a chance to run.
You should put the part.Transparency checks inside the loop, so they will run along with changing the transparency.
Also I’d recommend using TweenService instead of a lot of wait()'s.

2 Likes

local ts = game:GetService(“TweenService”) --tweenservice

local ti = TweenInfo.new(

3, --seconds

Enum.EasingStyle.Linear,–easing style

Enum.EasingDirection.Out,–easing direction

0,–repeat count

true–reverses

)

local tween = ts:Create(part, ti, {Transparency = 1})

tween:Play()

that is the tween code and to check the part’s transparency insert another script into the part with this code:

script.Parent:GetPropertyChangedSignal(“Transparency”):Connect(function()

–your statements here

end)

2 Likes

Your code stays in this loop forever, so the code after the loop will never run. You can fix the issue a few different ways:

  • Use TweenService to automate the animation looping forever
  • Wrap the loop in task.spawn (run code seperatly)
  • Use two scripts (not recommended)
1 Like

u can check in the bruh(kill) script the transparency

local part = script.Parent

function OnTouched(hit)
if hit.Parent:FindFirstChild("Humanoid") then
 if part.Transparency == 1 or part.Transparency == .6 or part.Transparency == .7 or part.Transparency == .8 or part.Transparency == .9 then 
     return
end
     hit.Parent.Humanoid.Health = 0 -- make the player die
   end
end

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

doesn’t work for some reason?
(rvino2rivuno2i3ruvn3)

is the part transparency not changing or is the detection systems isn’t working?

what didnt work the transparency detection or the tween?

local part = script.Parent

function OnTouched(hit)
if hit.Parent:FindFirstChild("Humanoid") then
 if part.Transparency == 1 or part.Transparency == .6 or part.Transparency == .7 or part.Transparency == .8 or part.Transparency == .9 then 
     return
end
     hit.Parent.Humanoid.Health = 0 -- make the player die
   end
end

script.Parent.Touched:Connect(OnTouched)

local transparency = 0
local direction = false

while wait(0.1) do
    if transparency < 1 and direction == false then
        transparency = transparency + 0.1
    elseif transparency > 0 and direction == true then
        transparency = transparency - 0.1
    end

    part.Transparency = transparency

    if transparency >= 1 then
        direction = true
    elseif transparency <= 0 then
        direction = false
    end
end

part transparency not working
(ovqn34urvnieuvrn)

where you put the script???

replace “Part” on tween line by the route to your part

local part = script.Parent

coroutine.resume(coroutine.create(function()
   while true do
     for i = 0, 1, 0.1 do
       script.Parent.Transparency = i
       wait(0.1)

       if part.Transparency >= 0.6 then
          part.bruh.Enabled = false
       else
          part.bruh.Enabled = true
       end

       if i == 1 then
         if i == 1 or i <= 0.6 then
            part.CanCollide = false
         end
         for i = 1, 0, -0.1 do
           script.Parent.Transparency = i
           wait(0.1)
         end
       end
     end
   end
end))

Hopefully this works for you!