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

Button can still be pressed even after variable is set?

In this code, the button will still be pressed and will glitch out of the world even after already being pressed, and also it looks horrible in game.

local hit = script.Parent.Touched:Connect(function(hit)
local y = 0.5
local touchedalready = false
local position = script.Parent.Position
local prevy = position.Y
script.Parent.Touched:Connect(function(hit)
	if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") and touchedalready == false then
		touchedalready = true
		script.Parent.Material = Enum.Material.SmoothPlastic
		while y > 0 do
			script.Parent.CFrame = CFrame.new(position.X,position.Y - 0.02,position.Z)
			wait(0.01)
			y -= 0.02
			print(touchedalready)
			end
		end
	end)
end)

1 answer

✓ Accepted answer

Try adding a NumberValue called y under your button part, and then set its value to 0, then use this code. Lua seems weird about changing variables in while statements and it is still a mystery to me, but adding values into the workspace always seems to fix it.

local hit = script.Parent.Touched:Connect(function(hit)
	local y = script.Parent.y
	local position = script.Parent.Position
	local prevy = position.Y
	script.Parent.Touched:Connect(function(hit)
		script.Parent:WaitForChild("TouchInterest"):Destroy()
		if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
			script.Parent.Material = Enum.Material.SmoothPlastic
			while y.Value < 0.5 do
				script.Parent.CFrame = CFrame.new(position.X,position.Y - y.Value,position.Z)
				wait(0.01)
				y.Value += 0.02
			end
		end
	end)
end)

1 comment

0

Thank you, this is very important since my whole game is about buttons.

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