Tool key-press activation

I have a tool that is supposed to play sound A when the left mouse button is clicked, and sound b when for example key L is pressed down. For now I only got the sound A click-activated function working, but I can not find a solution on how to make the sound B function.

This is the current script:

local Tool = script.Parent;

Tool.Enabled = true

function onActivated()
	if not Tool.Enabled  then
		return
	end
	
	soundA:Play()
	Tool.Enabled = true

end

function onEquipped()
	soundA = Instance.new("Sound")
	soundA.SoundId = "http://www.roblox.com/asset/?id=bla" 
	soundA.Parent = Tool
	soundA = Instance.new("Sound")
	soundA.SoundId = "http://www.roblox.com/asset/?id=bla" 
	soundA.Parent = Tool
end

function onUnequipped()
end

script.Parent.Activated:connect(onActivated)
script.Parent.Equipped:connect(onEquipped)
script.Parent.Unequipped:connect(onUnequipped)

I have tried using some things like ContextActionService but never managed to get them working. Any help is more than welcome, thanks in advance!

4 Likes

In your current script, you don’t have the button you want to press in the script. Add local script.MouseButton1 = LeftClick above the soundA and local script.MouseButton1 = L above soundB. Try that.

1 Like

Like this?

local Tool = script.Parent;

Tool.Enabled = true

local script.MouseButton1 = LeftClick 
function onActivated()
	if not Tool.Enabled  then
		return
	end
	
	soundA:Play()
	Tool.Enabled = true
	
	local script.MouseButton1 = L
function onActivated()
	if not Tool.Enabled  then
		return
	end
		
	soundB:Play()
	Tool.Enabled = true

end

function onEquipped()
	soundA = Instance.new("Sound")
	soundA.SoundId = "http://www.roblox.com/asset/?id=bla" 
	soundA.Parent = Tool
	soundA = Instance.new("Sound")
	soundA.SoundId = "http://www.roblox.com/asset/?id=bla" 
	soundA.Parent = Tool
end

function onUnequipped()
end

script.Parent.Activated:connect(onActivated)
script.Parent.Equipped:connect(onEquipped)
script.Parent.Unequipped:connect(onUnequipped)
1 Like

I think it would be best to use ContextActionService here. That way, you can bind various actions to your tool being equipped. For example:

local ContextActionService = game:GetService("ContextActionService")

local tool = script.Parent
local actionSoundA = "SoundA"

local function playSound(actionName, inputState, inputObject)
	if actionName == actionSoundA and inputState == Enum.UserInputState.Begin then
		local soundA = Instance.new("Sound")
		soundA.SoundId = "http://www.roblox.com/asset/?id=7753420371" 
		soundA.Parent = tool

		soundA:Play()
	end
end

tool.Equipped:Connect(function()
	ContextActionService:BindAction(actionSoundA, playSound, false, Enum.KeyCode.E)
end)

tool.Unequipped:Connect(function()
	ContextActionService:UnbindAction(actionSoundA)
end)

If you’d like to see more details on ContextActionService and all the parameters, here’s a link to the page on it: ContextActionService

Edit: In this case, sound A will play if the player is holding the tool and press the ‘E’ key. You can change this by swapping out Enum.KeyCode.E with whatever you want to trigger the sound. For example, Enum.KeyCode.H would play it with the ‘H’ key. For mouse/touch input, use UserInputType.

4 Likes