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

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

Another solution would be to check
if Tool.Parent == Player.Character then
ur code
end

1 Like

Replace the declarations with this:

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

NOTE: I forgot waitforchild as the script runs at runtime but the objects don’t load until a little after runtime.

Good news it got rid of that error, bad news no animation is playing at all and says thisfixed