ContextActionService Help

I am using this local script to attack NPC and play attack animation at the same time, but I am not smart enough to bind it with a ContextActionService.
Can anybody please help me to convert this script into ContextActionService:

local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Root = character:WaitForChild("HumanoidRootPart")
local Animation = character:WaitForChild("Humanoid"):LoadAnimation(script.Attack)
-- Seattings
local damage = 10
local Debounce = 1
local Keybind = Enum.KeyCode.F
local CanPunch = true





UserInputService.InputBegan:Connect(function(input,busy)
	if input.KeyCode == Keybind and not busy then
		if CanPunch == true then
			CanPunch = false
			Animation:Play()
			Animation.Looped = false
		game.ReplicatedStorage.Punch:FireServer(damage)
		print("4444444")
			wait(Debounce)
CanPunch = true
end
end
	end)

Heres how youd bind your function with ContextActionService. You can add the rest of the values yourself :slight_smile:

local Context = game:GetService("ContextActionService")


local function AttackFunc(Name,State,Input)
	
	if Name == "Attack" and State == Enum.UserInputState.Begin and CanPunch then
		CanPunch = false
		
		CanPunch = false
		Animation:Play()
		Animation.Looped = false
		game.ReplicatedStorage.Punch:FireServer(damage)
		print("4444444")
		task.wait(Debounce) -- // "wait()" is deprecated. Use "task.wait()" instead
		CanPunch = true
		
	end
	
end



	
Context:BindAction("Attack",AttackFunc,false,Enum.KeyCode.F) -- // Third argument decides if the service should create a button for mobile users
1 Like

Thank you so much, but when I am trying to add a button in the Mobile UI with these additional lines at the bottom, it is not showing up. There should be a blank circle button.

local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Root = character:WaitForChild("HumanoidRootPart")
local Animation = character:WaitForChild("Humanoid"):LoadAnimation(script.Attack)
-- Seattings
local damage = 10
local Debounce = 1
local Keybind = Enum.KeyCode.F
local CanPunch = true





local Context = game:GetService("ContextActionService")


local function AttackFunc(Name,State,Input)

	if Name == "Attack" and State == Enum.UserInputState.Begin and CanPunch then
		CanPunch = false

		CanPunch = false
		Animation:Play()
		Animation.Looped = false
		game.ReplicatedStorage.Punch:FireServer(damage)
		print("4444444")
		task.wait(Debounce) -- // "wait()" is deprecated. Use "task.wait()" instead
		CanPunch = true

	end

end




Context:BindAction("Attack",AttackFunc,false,Enum.KeyCode.F)
Context:SetImage("Attack", "rbxassetid://0")
Context:SetPosition("Attack", UDim2.new(0.5, 0,0.7, 0))



1 Like

You forgot to change the third arg to true. If its false it wont create a button. If its true it will :slight_smile: should have mentioned that in my other post that was an error on my end sorry :sweat_smile:

1 Like

Thank you so much, well I am new to scripting, I should have figured out that last part by myself. :smiley:

1 Like

Haha dont worry about it


1 Like