Context does not work

so basically I have a function that connects the binds the context after the player equips the tool

but the problem is it doesnt work it doesnt print out 1 but it prints out if i move the print outside the if condition

local SET_BEAM = "Set Beam"
local SETBEAM_KEY = Enum.KeyCode.I

local function onSetBeam(actionName,inputState,inputObj)
	if isEquipped == false then return end
	
	if inputState == Enum.UserInputState.Begin then
		print(1)
	end
	
end


--// Equip Functions
Tool.Equipped:Connect(function()
	isEquipped = true
	Equip_Anim:Play()
	
	Equip_Anim.KeyframeReached:Connect(function(keyFrame)
		if keyFrame == "Finish_Equip" then
			Idle1_Anim:Play()
			
			-- Bind Contexts
			print("bind")
			ContextActionService:BindAction(SET_BEAM,onSetBeam,false,SETBEAM_KEY)
			
		end
	end)
	
end)

Can you confirm whether did this KeyframeReached event gets fired?

yes it gets fired the print(“bind”) does print Ive worked with contextactionservice before and I know that should work I even checked my previous scripts on contextactionservice and its the same but somehow on the script it doesnt work

IsEquipped will never not equal false since it’s scope is in the Equipped Event. To fix this, you’ll have to move it to the SETBEAM variable

I’m kinda confused on how the UserInputState.Begin. Could you tell us how this gets fired or how could we achieve this enumeration?

Or did you try pressing I on your keyboard?

Heres the whole code IsEquipped is on the scope

--// Services

local Players = game:GetService("Players")

local ContextActionService = game:GetService("ContextActionService")

--// Player

local LPlayer = Players.LocalPlayer

local Char = LPlayer.Character or LPlayer.CharacterAdded:Wait()

local Humanoid = Char:WaitForChild("Humanoid")

LPlayer.CharacterAdded:Connect(function(char)

Humanoid = char.Humanoid

end)

--// Directories

local Tool = script.Parent.Parent

local Handle = Tool:WaitForChild("Handle")

local Handlers = Tool:WaitForChild("Handlers")

local Events = Handlers:WaitForChild("Events")

local Animations = Handlers:WaitForChild("Animations")

--// Events

local SetBeam = Events:WaitForChild("SetBeam")

--// Context

local CHANGE_FORM = "Change Form"

local SET_BEAM = "Set Beam"

--// Keys

local CHANGEFORM_KEY = Enum.KeyCode.K

local SETBEAM_KEY = Enum.KeyCode.I

--// Animations

local Idle1_Anim = Humanoid.Animator:LoadAnimation(Animations:WaitForChild("Idle_1"))

local Equip_Anim = Humanoid.Animator:LoadAnimation(Animations:WaitForChild("Equip"))

--// SFX

local IgniteSFX = Handle:WaitForChild("Ignite")

local DisIgniteSFX = Handle:WaitForChild("DisIgnite")

local ClashSFX = Handle:WaitForChild("Clash")

local HitSFX = Handle:WaitForChild("Hit")

local HumSFX = Handle:WaitForChild("Hum")

local Swing1SFX = Handle:WaitForChild("Swing_1")

local Swing2SFX = Handle:WaitForChild("Swing_2")

--// Values

local isEquipped = false

local isIgnite = false

local currentForm = 1

--// Functions

local function onSetBeam(actionName,inputState,inputObj)

if isEquipped == false then return end

if inputState == Enum.UserInputState.Begin then

print(1)

end

end

--// Equip Functions

Tool.Equipped:Connect(function()

isEquipped = true

Equip_Anim:Play()

Equip_Anim.KeyframeReached:Connect(function(keyFrame)

if keyFrame == "Finish_Equip" then

Idle1_Anim:Play()

-- Bind Contexts

print("bind")

ContextActionService:BindAction(SET_BEAM,onSetBeam,false,SETBEAM_KEY)

end

end)

end)

Tool.Unequipped:Connect(function()

isEquipped = false

isIgnite = false

Equip_Anim:Stop()

Idle1_Anim:Stop()

--// Unbind Contexts

ContextActionService:UnbindAction(SET_BEAM)

end)

when you press the key that was set to the context it gets fired

uhmm so this is weird when I tried this


	if inputState == Enum.UserInputState.End then
		print(2)
	end

it does print(2) but UserInputState.Begin does not fire

Update so I changed the keycode for the context

first i tried key “I” still doesnt work then I tried key “O” doesnt work as well but when I tried key “Y” it worked! idk why but it worked

Could you maybe print out what is the input state of inputState like so:

print(inputState.UserInputState)

I think I know why.

The keys I and O are reserved for zooming in and out of your camera in Roblox scripts. This might be the issue.

1 Like

ohhh I see never knew that… well thats probably why

1 Like