How can i make keybinds for my game?

So, i’ve been trying to make a combat game and would like some help on how to make functions be connected to a keybind.

Im aware of UserInputService, however i do not know how fully to use it, i’ve tried connecting a function into a InputBegan event like this:

local uis = game:GetService("UserInputService")

local function ExampleFunction(input, game_processed)

--some code here

end

uis.InputBegan:Connect(ExampleFunction)

It would be very helpful if somebody could assist me!

1 Like

I think UserInputService or ContextActionService is the way to go with this.

UIS:

local uis = game:GetSevice("UserInputService")

local function process(input:InputObject, processed:boolean)
    if processed then return nil end --if they interacted with a GUI element
    if input.KeyCode == Enum.KeyCode.A then --if they pressed the A key
        print("Key 'A' was pressed.")
    end
end

uis.InputBegan:Connect(process)

CAS:

local cas = game:GetService("ContextActionService")

local function process()
    print("The 'A' key was pressed.")
end

cas:BindAction("Something", process, true, Enum.KeyCode.A)
--[[
Parameter 1 is the name
Parameter 2 is the subprogram to bind
Parameter 3 is whether a mobile touch button should be created
Paramater 4 is any UserInputTypes to bind
]]

Please note you can only use these on the client.

Here’s the documentation for both:
UserInputService
ContextActionService

4 Likes

I’d recommend CAS (ContextActionService). You can control the input flow and the order which inputs are checked and processed and easily disconnect them from other scripts on the same machine. Just as @12345koip mentioned.

So, i tried what you suggested and ContextActionService seems like the best for the type of code im trying to make, however i encountered some errors while using this:

  • 12:55:48.739 ContextActionService: Unexpected error while invoking callback: Workspace.unturnedfan505.LocalScript:41: attempt to index nil with ‘Parent’ - Studio*
  • 12:55:48.870 Workspace.unturnedfan505.LocalScript:41: attempt to index nil with ‘Parent’ - Cliente - LocalScript:41*

local DAMAGE = 25

-- local KNOCKBACK = 30 

-- local COOLDOWN = 2.5

local char = script.Parent
local humanoid = char:FindFirstChildOfClass("Humanoid")
local root = char:FindFirstChild("HumanoidRootPart")
local anim = script.Melee
local animator = humanoid:FindFirstChildOfClass("Animator")


local cas = game:GetService("ContextActionService")

local function Hit()
	
	--load animation
	animator:LoadAnimation(anim)

	local part = Instance.new("Part")
	part.Parent = workspace
	part.Size = Vector3.new(6, 6, 5)
	part.Transparency = 0.5 --set to 1 to disable visualizer
	part.Massless = true
	part.Anchored = false
	part.CastShadow = false
	part.CanCollide = false
	part.CFrame = root.CFrame * CFrame.new(0, 0, ((part.Size.Z/2) + 0.5) * -1)

	local weld = Instance.new("WeldConstraint")
	weld.Parent = part
	weld.Part0 = root
	weld.Part1 = part

	local PartsInside = workspace:GetPartsInPart(part)

	local function hitboxfunc(v)

		local char = v.Parent
		local hum = char:FindFirstChildOfClass("Humanoid")	

		hum:TakeDamage(DAMAGE)
		print('damaged'..char.Name..'')
		
	end
	
	hitboxfunc()
	task.wait(anim)
	
end

cas:BindAction("Melee", Hit, true, Enum.KeyCode.E)

In this script, a part is created that is used as a Hitbox for the Melee. in the “hitboxfunc” function, it will search for a humanoid, which if it finds it, it will damage the humanoid by the amount of the DAMAGE value.

I should have probably shown the code when i made the post instead of replying with it, sorry!

You’re passing no paramater, so v is nil.

You don’t need a subprogram for this anyway.

1 Like

By subprogram do u mean the function or? (Im new to scripting so these terms are kinda new to me.)

Here’s a post I made about subprograms and parameters to help someone else:
What are parameters and why should I use them? - Help and Feedback / Scripting Support - Developer Forum | Roblox

So i should remove the v subprogram? But i dont understand how that would work, because for the hitbox to damage the humanoid it needs to find a char, which i used v.Parent.

Yeah, you could just get rid of it and use the pre-existing char variable if you are referencing the same thing.

I could, but the thing im looking for is the character who is touching the hitbox part. using the pre-existing char variable would make it (if im assuming this correcly) damage the character whos using the melee/doing the attack.

Where exactly are you getting the seperate character from?

Basically, it when runs local PartsInside = workspace:GetPartsInPart(part) , it will get a limb from the character and not the character model itself. and in order to get the character, i did v.Parent, because the character’s limb’s parent is the character model.

Its my first time using workspace:GetPartsInPart(example), so im not sure if im using them correctly.

Okay, i fixed the errors, however theres one issue. When i punch, the hitbox detects me and the workspace, but if i do :TakeDamage() then im gonna hit myself with the hitbox, i could fix this by making the hitbox a little farther then the character can reach, but it wouldnt fix the issue with it detecting the Workspace.

If you’re doing this on the client, don’t. Making other clients take damage will not replicate. Also when you say “Detecting the workspace”, what do you mean?

It sounds like you need to raycast instead of using GetPartsInPart.

I was using print('Hitbox Touched '..v.Parent.Name..'') to print out which parts are getting detected. When i refered to “detecting the workspace”, the print() said that “Hitbox Touched Workspace”

But yeah, you’re probably right. Im gonna consider using something like Raycasts because it seems easier to make.