Keybind To Toggle Script

Hello person reading this! I’m trying to make a toggleable script where if I hit a certain key bind it will activate a script to point with a specific arm.

I’ve been searching and trying to script for a while but cant figure out how to do it. (It might be simple I’m just a beginner)

Here is the script:

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local plr = Players.LocalPlayer
local char = plr.Character
local mouse = plr:GetMouse()

local armOffset = char.UpperTorso.CFrame:Inverse() * char.RightUpperArm.CFrame

local armWeld = Instance.new("Weld")
armWeld.Part0 = char.UpperTorso
armWeld.Part1 = char.RightUpperArm
armWeld.Parent = char

RunService.Heartbeat:Connect(function()
	local cframe = CFrame.new(char.UpperTorso.Position, mouse.Hit.Position) * CFrame.Angles(math.pi/2, 0, 0)
	armWeld.C0 = armOffset * char.UpperTorso.CFrame:toObjectSpace(cframe)
end)

If someone could explain to me how to make this toggleable I would really be gratefull.
Thank you!

1 Like

Im just a beginner so i can barely ready this script, but from what ive learned (In my opinion) the best way to make things togglable is to use a bool value

Like,

local bool = false

if bool == false then
--do stuff
bool = true
else
--do stuff
bool = false
end

The if statement will run, making the bool value true, and if you click the key again, it’ll l=skip over the if statement and look at the elseif statement since bool is true.

1 Like

don’t worry i took this script off a YouTube tutorial and just edited some minor stuff

Do you know how to connect setting the bool to true or false to a keybind?

1 Like

Using the UserInputService

local UIS = game:GetService('UserInputService') --Gets the UserInputService
local bool = false
UIS.InputBegan:Connect(function(input) --connects a function to the input
if input.KeyCode == Enum.KeyCode.Q then-- Detects if the player pressed Q. The keycode doesnt have to be Q it can be whatever you want as long as its on your keyboard 
if bool == false
--do stuff
bool = true
else
-do stuff
bool = false
end
end)
3 Likes

this will work in a local script correct? Also TYSM.

1 Like

Yes it will work in a local script!

You’re welcome, but im not entirely sure if it’ll work :sweat_smile:

1 Like

alright tysm! i find very kind people on this community :smiley:

1 Like

hmm isnt working RN so ill work on other stuff in da game for now but TYSM ill try fixing it somehow :smiley:

1 Like

Where did you place the local script?