im tryin to make it so when you touch a part, it becomes transparent and when it becomes transparent it will become completely invisible

Viewed 36
local e = "asdusad"
    game.Workspace.Part.Touched:Connect(function()
        game.Workspace.Part.Transparency = .7
        if game.Workspace.Part.Transparency == .7 then
            e = true
            if e == true then 
                game.Workspace.Part.Transparency = 1
            elseif e == false then
                game.Workspace.Part.Transparency = 0
            end
        end
    end)

in roblox studio, it doesnt show that theres any errors but it doesnt work

1 Answers
local e = "asdusad"
game.Workspace.Part.Touched:Connect(function()
    game.Workspace.Part.Transparency = .7
    if game.Workspace.Part.Transparency == .7 then
        e = true
        if e == true then 
            game.Workspace.Part.Transparency = 1
        elseif e == false then
            game.Workspace.Part.Transparency = 0
        end
    end
 end)

How does this code make any sense logic-wise?

You assign game.Workspace.Part.Transparency = .7.

Then if game.Workspace.Part.Transparency == .7 (which is always true because you just assigned that value) you assign e = true and after that if e == true (which again is always true as you assigned that value) you assign game.Workspace.Part.Transparency = 1.

This code is equivalent to:

local e = "asdusad"
game.Workspace.Part.Touched:Connect(function()
   e = true
   game.Workspace.Part.Transparency = 1
end)

Everytime this event handler is triggered you make the object opaque.

You need to only make it transparent if it is opaque and make it opaque only if it transparent.

Related