Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Trying to make it so an attack only works when I'm holding a certain object?

I'm trying to make it so I can't use the tools attacks when I'm not using the tool but I can't figure out how

I tried doing "if.Script.Parent.Equipped then" but I was still able to use the moves when I didn't have the tool equipped

if script.Parent.Equipped then
	
	local uis = game:GetService("UserInputService")

	uis.InputBegan:Connect(function(Input)
		local MouseImput = Input.UserInputType --Get all mouse inputs--
		local KeyCode = Input.KeyCode --Get all keyboard inputs--

		if MouseImput == Enum.UserInputType.MouseButton1 then
			local animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Attack) 	--If left click then play attack animation--
			animation:Play()
			script.Disabled = true
			wait(1.0)
			script.Disabled = false

		elseif KeyCode == Enum.KeyCode.Z then
			local animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Bat)
			animation:Play()
			script.Disabled = true
			wait(1.5)
			script.Disabled = false

		elseif KeyCode == Enum.KeyCode.X then
			--not done yet--
		end
	end)
end

any help?

1 answer

You will need to use Context Action Service rather than UserInputerService. This is because CAS allows you to bind and unbind an input.

An example would be)

local CAS = game:GetService("ContextActionService")

local tool = script.Parent

local function attack()
	--Whatever actions you want your tool to do
end

tool.Equipped:Connect(function()
	CAS:BindAction("NameOfString", attack, false, Enum.KeyCode.F)
--[[
The parameters are 
1) String to recognize the name of action(could be anything you want, but make sure :BindAction and :UnbindAction have the same name for your string.
2)The function you want to execute
3)If you are on mobile and want to add a button, put true, otherwise, false
4) What key do you want to be pressed(For this example, I use the F key.
]]
end)

tool.Unequipped:Connect(function()
	CAS:UnbindAction("NameOfString")
end)

1 comment

0

this worked, thank you so much!

This discussion is preserved as it was. New answers, comments, and votes are closed.