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

UserInputService only when holding a Tool?

So i have been trying this for a long time and still can't do it.How do i make it so that when the player is holding a tool THEN they can use UIS (UserInputService).Most of the time i can use the UIS without holding the tool and that is not what i am trying to get.

wait(3)
local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer 
local Holding = false

if script.Parent.Equipped == true then
	Holding = true
elseif script.Parent.Equipped == false then
	Holding = false
end

	UIS.InputBegan:Connect(function(key)
		if key.KeyCode == Enum.KeyCode.R and Holding == true then
			print("Key")
			 local BV = Instance.new("BodyVelocity", Player.Character.HumanoidRootPart)
		BV.maxForce = Vector3.new(0, math.huge, 0)
		BV.Velocity = Vector3.new(0, 60, 0)
		wait(0.5)
		BV:Destroy()
		end
	end)

2 comments

0

Not sure if this'll work but try script.Parent.Equipped:connect(function() Holding = true end) and the script.Parent.Unequipped:connect(function() Holding = false end)

0

Doesn't work. I tried it before.

2 answers

✓ Accepted answer

I figured out the problem here.

There's a setting within the tool called 'RequiresHandle'. If this is enabled, the tool will wait for a handle and will yield the script.

If you don't want a handle, make sure that's disabled or else include a part within the tool called 'Handle'.

local UIS 		=		game:GetService("UserInputService")
local Tool		=		script.Parent

local Equipped	=		false

UIS.InputBegan:connect(function(Input, GP)
	if Input.UserInputType == Enum.UserInputType.Keyboard then
		if GP ~= true then
			if Equipped == true then
				local Key = Input.KeyCode
				
				if Key == Enum.KeyCode.R then
					print("R was pressed!")
				end
				
			end
		end
	end
end)

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

3 comments

0

If this worked for you please mark it as the solution. If it didn't, however, please comment down what happened. Thanks.

0

Thank you very much! You have saved me. (if you are wondering,I am making a Magic tool with multiple moves and since theis no handle i forgot to uncheck itre )

0

No problem.

Hello. Your variable Holding will be defined once and it will not change if you equip/unequip the tool. There are 2 events that will help you: Equipped and Unequipped. Here's an example:

local UserInputService = game:GetService("UserInputService")
local Holding = false

UserInputService.InputBegan:Connect(function(input, box)
	if input.KeyCode == Enum.KeyCode.R and not box and Holding then
		print("Key R pressed, tool equipped.!")
	end
end)

script.Parent.Equipped:Connect(function()
	Holding = true
end)

script.Parent.Unequipped:Connect(function()
	Holding = false
end)

Note: box is a boolean value. If the user was focused on a TextBox, for example, the Chat, when he was pressing the key, it will be true, if not, false.

7 comments

0

Wish i could upvote but i need 25 reputation :(

0

It apparently does not work.

0

Tell me what errors do you have in the output?

0

Also, are you running it in a Script or LocalScript?

0

No errors,i tried running in Script and Local Script

0

Run it in a LocalScript and make sure the LocalScript is direclty inside the tool. Not any descendants the tools has.

0

Did exactly what you said and it still didn't work.

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