Allowing Keybinding to work when you only have a certain tool equipped

I have this script where if you click Z it will play a animation, this is suppose to be a special katana slash.
this is only suppose to work when you have the katana in your hand but the animation plays whenever you want. this is what I have so far and if anyone could help me that would be great

2 Likes

You could add a variable and you could connect Tool.Equipped event with that and add a check for that in the InputBegan.

Can you please give me a example? (you don’t have to right out the entire script I am just trying to get a better understanding

Sure,
This is how I would do it:
Code

You could also use the ContextActionService and bind all of your functions on tool equip and unbind on unequip

1 Like

I asked the other guy to give me a visual example of what they meant so would you mind doing the same (once again don’t have to do it to my script just give me a example of what you meant)

is there a specfic way I would have to put this? or is this just the way it would be done? and if so would I just have to add my toolpath?

You would just put the tool path in and add

UserInputService.InputBegan:Connect(function(Input, IsTyping)
   if ToolEquipped then
        --other stuff that was in there
   end
end

[make sure to wrap the other stuff with that if statement]

2 Likes

Sure, something like

local CAS = game:GetService("ContextActionService")

local AllowedInputs = {Enum.KeyCode.Z}

local ACTION_NAME = "SomeAction"

function DoAction(actionName, inputState, inputObject)
	if actionName == ACTION_NAME then
		if inputState == Enum.UserInputState.Begin then
			-- do stuff
		end
	end
end

Tool.Equipped:Connect(function()
	CAS:BindAction(ACTION_NAME, DoAction, false, table.unpack(AllowedInputs))	
end)

Tool.Unequip:Connect(function()
	CAS:UnbindAction(ACTION_NAME)
end)

You can also bind different actions to different functions, or even the same function since it passes the action name, but the context action service is usually regarded as better for compatibility cross platform

1 Like

Ok one more thing and ima try these methods out, how do I redriect to startpack? and then other than that Ima test it out and see if it works!

Well roblox sort of makes this hard. You would have to parent the script to the tool and reference it with script.Parent

Sorry but at this point I am kinda lost.

This is new script:

local Player = game.Players.LocalPlayer
local Character = Player.Character or script.Parent.Parent.Parent
local Humanoid = Character.Humanoid
local UserInputService = game:GetService('UserInputService')
local Tool = script.Parent
local ToolEquipped = false

local AnimationID = 'rbxassetid://7001909669'
local Debounce = true
local Key = 'Z'

Tool.Equipped:Connect(function() ToolEquipped = true end)
Tool.Unequipped:Connect(function() ToolEquipped = false end)

UserInputService.InputBegan:Connect(function(Input, IsTyping)
	if ToolEquipped then
		if IsTyping then return end
		local Animation = Instance.new('Animation')
		Animation.AnimationId = AnimationID
		local LoadAnimation = Humanoid:LoadAnimation(Animation)
		LoadAnimation:Play()
		wait(1)
		Animation:Destroy()
		Debounce = true
	end
end)
1 Like

Equipped is not a valid member of Model “Workspace.EvanNicholasRBLX”
do I have to bind the script to the tool in any way?

Yes, parent the script to the tool.
It should work after that.

so would the scripts parent be the tool? if so it can’t get the humanoid with the way its directed (Character.Humanoid) or did I flip the way I’m suppose to do it

In the script, I changed the declaration of Player, Humanoid, and Character.

so if I make the scripts parent the tool it should just work? because if thats the case it didn’t work.
which one of these should it be because either one comes up as errors this this2

1 Like

The local script should be a child of the Tool. The one on the left is the correct one.

did that and this pops up