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

Button can still be pressed even after variable is set?

Asked by 2 years ago

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

Log in to vote
0
Answered by 2 years ago

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)

0
Thank you, this is very important since my whole game is about buttons. MrBucketOfficial 39 — 2y
Ad

Answer this question