#I just wanna know how to make a toggle / flip flop in lua
1 messages · Page 1 of 1 (latest)
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
First one is basically the way to go
noooo, alright so uh how do i use it in a function then
--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
Where's the boolean?
i did not have a boolean set, should i add one?
Like what are you toggling
toggling viewCompass, the compass rotating, and the compass clicking
Im on a bus and I've lost all sense of everything