Help with toggle function with context action service

I want it so when you press v on your keyboard it turns on a flashlight that is in your right arm using context action service, but everything I’ve tried doesn’t work.

When I press v for the first time, the flashlight appears then disappears again and when I press v again it just doesn’t work for some reason.

Before this script, I tried making two functions and two other binds and then unbind the one that gives you the flashlight when you have it and bind it again when you don’t, but that ignored the removing flashlight bind for some reason. I have looked in the dev forum but nothing really helped me.

local player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local char = script.Parent or player.CharacterAdded:Wait()
local humanoid = char.Humanoid
local isrunning = false
local RS = game:GetService("ReplicatedStorage")
local rightarm = char:WaitForChild("Right Arm", 10)
local FL = RS.FLashLight
local CAS = game:GetService("ContextActionService")


local FL2
local motor6d
function FlashLight(actionname, inputstate, inputobj)
	if inputstate == Enum.UserInputState.Begin then
		if not FL2 then
			FL2 = FL:Clone()
			motor6d = Instance.new("Motor6D", rightarm)
			motor6d.Part0 = rightarm
			motor6d.Part1 = FL2.Handle
			FL2.Parent = rightarm
		end
	elseif FL2 then
		if inputstate == Enum.UserInputState.End then
			FL2:Destroy()
			motor6d:Destroy()
		end
	end
end


function running(actionname, inputstate, inputobj)
	if inputstate == Enum.UserInputState.Begin then
		isrunning = true
		if isrunning then
			humanoid.WalkSpeed = 18
		end
	elseif inputstate == Enum.UserInputState.End then
		isrunning = false
		if not isrunning then
			humanoid.WalkSpeed = 12
		end
	end
end

CAS:BindAction("run", running, true, Enum.KeyCode.LeftShift)
CAS:BindAction("toggleflashlight", FlashLight, true, "v")

Ignore the running function and also if you have any idea how I can change the flahlight’s CFrame please do tell me.

1 Like

Looks like you need a debounce for V.

DB = true
UIS.InputBegan:Connect(function(input, value)
if input.KeyCode == Enum.KeyCode.V and DB = true then
DB = false
–code
else
if input.KeyCode == Enum.KeyCode.V and DB = false then
DB = true
–code
end
end

This stops it completely. Maybe I applied it incorrectly so, could you help me fix it?

local FL2
local motor6d
local DB = true
function FlashLight(actionname, inputstate, inputobj)
	if inputstate == Enum.UserInputState.Begin then
		if not FL2 and DB == true then
			DB = true
			FL2 = FL:Clone()
			motor6d = Instance.new("Motor6D", rightarm)
			motor6d.Part0 = rightarm
			motor6d.Part1 = FL2.Handle
			FL2.Parent = rightarm
			wait(2)
			DB = true
		end
	else
		if FL2 and DB == true then
			if inputstate == Enum.UserInputState.End then
				DB = false
				FL2:Destroy()
				motor6d:Destroy()
				wait(2)
				DB = true
			end
		end
	end
end

Try this

local FL2
local motor6d
local DB = true
function FlashLight(actionname, inputstate, inputobj)
if inputstate == Enum.UserInputState.Begin then
if not FL2 and DB == true then
DB = false
FL2 = FL:Clone()
motor6d = Instance.new(“Motor6D”, rightarm)
motor6d.Part0 = rightarm
motor6d.Part1 = FL2.Handle
FL2.Parent = rightarm
wait(2)
end
else
if FL2 and DB == false then
if inputstate == Enum.UserInputState.Begin then
DB = true
FL2:Destroy()
motor6d:Destroy()
wait(2)
end
end
end
end

Unfortanetly, this doesn’t work.

Check my code, make sure I didn’t input anything wrong. This should work, but if not, I hope someone else can help.

I did forget this, I put begin twice

I am having this same problem. It seems the function runs when the key is released as well as when it is initially pressed. I have yet to solve this.

1 Like

doesn’t work. I’ll just wait for someone else

I finally found a solution for the problem and it’s really simple

Updated code:

local player = game.Players.LocalPlayer
local UIS = game:GetService("UserInputService")
local char = script.Parent or player.CharacterAdded:Wait()
local humanoid = char.Humanoid
local isrunning = false
local RS = game:GetService("ReplicatedStorage")
local rightarm = char:WaitForChild("Right Arm", 10)
local FL = RS.FLashLight
local CAS = game:GetService("ContextActionService")
local has = false


local FL2
local motor6d
function FlashLight(actionname, inputstate, inputobj)
	if inputstate == Enum.UserInputState.Begin then
		if not has then
			FL2 = FL:Clone()
			motor6d = Instance.new("Motor6D", rightarm)
			motor6d.Part0 = rightarm
			motor6d.Part1 = FL2.Handle
			FL2.Parent = rightarm
			task.wait(0.3)
			has = true
		end
	end
	if inputstate == Enum.UserInputState.End and has then
		FL2:Destroy()
		motor6d:Destroy()
		task.wait(0.3)
		has = false
	end
end


function running(actionname, inputstate, inputobj)
	if inputstate == Enum.UserInputState.Begin then
		isrunning = true
		if isrunning then
			humanoid.WalkSpeed = 18
		end
	elseif inputstate == Enum.UserInputState.End then
		isrunning = false
		if not isrunning then
			humanoid.WalkSpeed = 12
		end
	end
end

CAS:BindAction("run", running, true, Enum.KeyCode.LeftShift)
CAS:BindAction("toggleflashlight", FlashLight, true, "v")
1 Like

I have come to the realization that there’s an equiptool function for the humanoid and I want to cry

1 Like