#I just wanna know how to make a toggle / flip flop in lua

1 messages · Page 1 of 1 (latest)

final shell
#

its really that simple i swear, i've been using stuff like [viewCompass.Enabled = not viewCompass.Enabled] but i can't make a function out of it

#

pleas im dyin out here

#

is there really not something better than

viewCompass.Enabled = not viewCompass.Enabled

cause i was thinking have a value like

local toggle = false

if tool.Activated then
  toggle = true
  if tool.Activated then
    toggle = false
end

but that seems complicated / bad nesting

gilded oxide
final shell
#

noooo, alright so uh how do i use it in a function then

final shell
#
--js some values
local tool = script.Parent
local toggle = true
local compAttachment = tool:FindFirstChild("CompAttachment")
local viewCompass = compAttachment:FindFirstChild("ViewCompass")
local theCompass = viewCompass:FindFirstChild("TheCompass")
--Sketchy camera magic
local player = game:GetService("Players").LocalPlayer
local camera = workspace.CurrentCamera
--Sounds
local soundService = game:GetService("SoundService")
local soundsFolder = soundService:FindFirstChild("Sounds4Later")
local compassSound = soundsFolder:FindFirstChild("CompassSound")
local compassClick = soundsFolder:FindFirstChild("CompassClick")
--not even done yet
local function opened()
    compassSound:Play()
    
end
--i could not even fathom what to do here
local function closed()
    viewCompass.Enabled = false
    
end

-- Toggles UI and plays CompassSound on click
tool.Activated:Connect(function()
    viewCompass.Enabled = not viewCompass.Enabled
    opened()
end)

if tool.Equipped == false then
    viewCompass.Enabled = false
end

local previousLookVector = nil
local lastClickTime = 0
local clickDebounce = 0.2 -- seconds
-- Updates compass rotation to face north, woooooooo
game:GetService("RunService").RenderStepped:Connect(function()
    if viewCompass.Enabled then
        local lookVector = camera.CFrame.LookVector
        local angle = math.atan2(lookVector.X, lookVector.Z)
        theCompass.Rotation = math.deg(-angle)

        -- Play CompassClick if lookVector changes
        if previousLookVector then
            if (lookVector - previousLookVector).Magnitude > 0.05 then
                local now = tick()
                if compassClick and (now - lastClickTime) > clickDebounce then
                    compassClick:Play()
                    lastClickTime = now
                end
            end
        end
        previousLookVector = lookVector
    else
        previousLookVector = nil
    end
end)

so this is my code

#

and as you can see im trying to fix it so its all in like functions, neat and organized

#

but for the life of me i could'nt figure out how to use that toggle in a function without it going kablewie

gilded oxide
#

Where's the boolean?

final shell
#

i did not have a boolean set, should i add one?

gilded oxide
#

Like what are you toggling

final shell
#

toggling viewCompass, the compass rotating, and the compass clicking

gilded oxide
#

Man im tired my brain isn't braining

#

I can try to help later

final shell
gilded oxide
#

Im on a bus and I've lost all sense of everything

final shell
#

nah i feel you man

#

i've been staring at this code for 7 minutes dumbfounded