Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How do I use a global variable?

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

✓ Accepted answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.