How to set a boolean value to true with a second mouse and back to false?

I have been trying to script a buttongui to set a boolean value to false and true like a switch, but I can’t really understand how. I am incredibly new to scripting and have no idea how to do it.
I have searched the devforum for answers but don’t understand it.
I want variable runs to become false once the buttongui is clicked, and become true again once buttongui is clicked again.

local ScreenGui=script.Parent
local START=ScreenGui.START
local runs = true
START.MouseButton1Click:Connect(function()
repeat
local randompos = math.random (-300, -100)
local randompos1 = math.random (120 ,320)
print (randompos, randompos1)
local PartCount=0
local spawnpart=Instance.new (“Part”)
spawnpart.Anchored=false
spawnpart.Position = Vector3.new (randompos1, 60, randompos)
spawnpart.Parent=game:GetService(“Workspace”)
game.ReplicatedStorage.PartCount.Value = game.ReplicatedStorage.PartCount.Value + 1
print(“Part count: “… game.ReplicatedStorage.PartCount.Value…””)
task.wait()
until runs == false
end)

for that sort of thing, using

VAR = not VAR

can be quite useful.

I’ve also changed the repeat until loop to a while loop, as it seems like it would be more applicable for this use case.

local ScreenGui=script.Parent
local START=ScreenGui.START
local runs = true
START.MouseButton1Click:Connect(function()
    runs = not runs
    while runs do
        local randompos = math.random (-300, -100)
        local randompos1 = math.random (120 ,320)
        print (randompos, randompos1)
        local PartCount=0
        local spawnpart=Instance.new (“Part”)
        spawnpart.Anchored=false
        spawnpart.Position = Vector3.new (randompos1, 60, randompos)
        spawnpart.Parent=game:GetService(“Workspace”)
        game.ReplicatedStorage.PartCount.Value = game.ReplicatedStorage.PartCount.Value + 1
        print(“Part count: “… game.ReplicatedStorage.PartCount.Value…””)
        task.wait()
    end
end)
1 Like

huh it works, what does runs = not runs do?

It functions as a switch. Think of it like this:

local enabled = false -- By default, enabled is false
enabled = not enabled -- This changes the value of enabled to the opposite of 'false', which is 'true'
-- 'enabled' is now true
enabled = not enabled -- This changes the value to the opposite once more
-- 'enabled' is now false again

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.