InputBegan not working

I have a function that does reload on inputbegan key R. But it doesnt work.

UIS.InputBegan:Connect(function(key, gameproccessed)
	print("key worked")
	if key == Enum.KeyCode.R and gameproccessed == false and Enabled == true then
		ShootEnabled = false
		print("Reload")
		script.Parent.Action:FireServer("Reload")
		wait(Config.SFX.Reload.TimeLength/2)
		CA = 0
		wait(Config.SFX.Reload.TimeLength/2)
		CA = APM
		MM = MM - 1
	end
end)

CA is current ammo
MM is max mags
APM is ammo per mag
Enabled is true on equipped, false on unequip.

I tried even setting it to JUST check if key is R but it still does not work\

I even tried
image

but it still ONLY prints
image
even when I press R

1 Like

oh wait its key.Keycode ohhhhhhhhhhhhhh

1 Like

I recommend using ContextActionService over UserInputService. It has better control. Here is how you would rewrite the script.

local CAS = game:GetService("ContextActionService")
CAS:BindAction("Reload", function(actionName, userInputState, inputObject)
	print("key worked")
	if key == Enum.KeyCode.R and Enabled == true then
		ShootEnabled = false
		print("Reload")
		script.Parent.Action:FireServer("Reload")
		wait(Config.SFX.Reload.TimeLength/2)
		CA = 0
		wait(Config.SFX.Reload.TimeLength/2)
		CA = APM
		MM = MM - 1
	end
end, false, Enum.KeyCode.R)
2 Likes

Ok I’lll make sure to use that

Hold on.

local CAS = game:GetService("ContextActionService")
CAS:BindAction("Reload", function(actionName, userInputState, inputObject)
	print("key worked")
	if key == Enum.KeyCode.R and userInputState == Enum.UserInputState.Begin and Enabled == true then
		ShootEnabled = false
		print("Reload")
		script.Parent.Action:FireServer("Reload")
		wait(Config.SFX.Reload.TimeLength/2)
		CA = 0
		wait(Config.SFX.Reload.TimeLength/2)
		CA = APM
		MM = MM - 1
	end
end, false, Enum.KeyCode.R)
1 Like