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

On and off button not working for a loop that I made multiple times why?

Asked by
2mania 14
2 years ago

Hi I am trying to make a lag test game and I already have a button to add parts but I was thinking about automation but I cant figure out how to fix my script if you can help me please reply

local on = false

if on == false and script.Parent.MouseButton1Click then 
    on = true
end


if on == true and script.Parent.MouseButton1Click then
    on = false
end

if not on == false then
    local Part = Instance.new("Part")
    Part.Position = Vector3.new(-41, 0.5, 73)
    Part.Parent = game.Workspace.Folder
    Part.Name = "Hi"
end

Here is the link to the game if you were wondering roblox.com/games/8482923831/lag-test-BE-CAREFUL

Thanks :)

0
Thanks a lot also you forgot to add the wait value on line nine 2mania 14 — 2y

1 answer

Log in to vote
0
Answered by 2 years ago

Use the MouseButton1Click event.

It is an event once a button is clicked.

local on = false




script.Parent.MouseButton1Click:Connect(function()
    on = not on -- Set to the opposite of it's current value.
    if on then
        repeat wait
            local Part = Instance.new("Part")
            Part.Position = Vector3.new(-41, 0.5, 73)
            Part.Parent = game.Workspace.Folder
                Part.Name = "Hi"
            wait(1)
         until on == false
    end
end)

You can use repeat wait until so that once on is set to true it makes a loop to make the part (again and again until it's not on).

Ad

Answer this question