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

How do I use a global variable?

Asked by 4 years ago
Edited by royaltoe 4 years ago

I'm trying to make a "button" that, when stepped on, causes another invisible platform to appear. I'm attempting to use a global variable to make it work, but I can't seem to get it. I've pasted the code from my two parts below:

Invisible block:


local visibility = game.Workspace.Platform.Transparency local Platform = game.Workspace.Platform local transparent = true function isTouched() print("hello") transparent = false game.Workspace.Platform.Transparency = 0 wait(1) game.Workspace.Platform.Transparency = 1 end game.Workspace.Platform.Touched:Connect(isTouched) while transparent == true do game.Workspace.Platform.Transparency = 1 end while _G.activated == true do print("On") game.Workspace.Platform.Transparency = 0 end Button: _G.activated = false function isTouched() print("hi") _G.activated = true wait(1) _G.activated = false end game.Workspace.Button.Touched:Connect(isTouched)

any advice would be appreciated. Thanks!

1 answer

Log in to vote
0
Answered by 4 years ago

The issue here isn't with the global variable; it's with your while loops. Currently the way you have it set up, it will keep looping until your condition is false. Once the condition is false, the loop stops and won't start again, even when the condition becomes true again.

What you could do instead is have checks inside of the loop, like this:

while wait() do
    if transparent == true then
        game.Workspace.Platform.Transparency = 1
    end

    if _G.activated == true then
        print("On")
        game.Workspace.Platform.Transparency = 0
    end
end
Ad

Answer this question