Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Door Transparency problem

Asked by 10 years ago

I made this Script, but whenever I touch it, it freezes ROBLOX so I think it is a HUGE Script. Does anyone know why this happens?

function onTouched(hit)
    wait(0.1)
    script.Parent.Transparency = script.Parent.Transparency + 1
    repeat until script.Parent.Transparency == 1
    wait(3)
    wait(0.1)
    script.Parent.Transparency = script.Parent.Transparency - 1
    repeat until script.Parent.Transparency == 0
end

script.Parent.Touched:connect(onTouched)

2 answers

Log in to vote
1
Answered by 10 years ago
-- original code
function onTouched(hit)
    wait(0.1)
    script.Parent.Transparency = script.Parent.Transparency + 1 -- this should be in the first repeat
    repeat
        wait(3) -- we don't need either of these waits since we have the one at the start of the function.
        wait(0.1)
        script.Parent.Transparency = script.Parent.Transparency - 1 -- Transparency will decrease. Since it's already lower than the until, it will loop infinitely.
    until script.Parent.Transparency == 1  -- To safeguard, rarely use ==, instead use >=, <=, <, or >.
    repeat
    until script.Parent.Transparency == 0 -- same case here as above
end

script.Parent.Touched:connect(onTouched)

Your problem lies in infinite loops. It's a good idea to safeguard by have a counter that breaks the loop if it loops more then a set number of times. You also had an extra repeat loop.

-- improved code, though likely not the absolute perfect version.
local rateOfChange = .01 -- Change this to however fast you want to door to fade. Think in percentages, 1 is 100%

function onTouched(hit) -- Define function "onTouched" with parameter "hit".
    if script.Parent.Transparency <= .5 then -- if the door if at least half opaque, then…
        repeat -- repeat
            script.Parent.Transparency = script.Parent.Transparency + rateOfChange -- door transparency = door transparency + rateOfChange
        until script.Parent.Transparency >= 1 -- loop until transparency is at least 1
    elseif script.Parent.Transparency > .5 then -- if the door is more than half transparent, then…
        repeat -- repeat
            script.Parent.Transparency = script.Parent.Transparency - rateOfChange -- door transparency = door transparency - rateOfChange
        until script.Parent.Transparency <= 0 -- loop until transparency is no more than 0
    else print"ERROR: Door transparency not recognized." -- if state not recognized, print an error.
    end --close the if statement
    wait() -- make sure it doesn't loop too fast
end --close the function

script.Parent.Touched:connect(onTouched) -- connect the touch event
Ad
Log in to vote
1
Answered by 10 years ago
local touchDebounce = false
function onTouched(hit)
    if touchDebounce then --If the door is already changing transparency,
        return nil --Stop the function.
    end
    touchDebounce = true --Tell the script that we're already in the process of changing the transparency.
    wait(0.1)
    repeat --Repeat the following code;
        script.Parent.Transparency = script.Parent.Transparency + 0.1 --Make the door less visible
        wait(wait()) --Stop the transparency increasing for a moment so that it's noticeable.
    until script.Parent.Transparency >= 1 --Stop the repeat when the door is invisible.
    wait(3)
    wait(0.1)
    repeat --Repeat the following code;
        script.Parent.Transparency = script.Parent.Transparency - 0.1 --Make the door more visible
        wait(wait()) --Stop the transparency decreasing for a moment so that it's noticeable.
    until script.Parent.Transparency =< 0 -Stop the repeat when the door is visible again
    touchDebounce = false --We're done here.
end

script.Parent.Touched:connect(onTouched)

Answer this question