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

Detect if a player press a key when the tool is equipped?

Hello I would like to make a skill for a game only work when the tool is equipped so I wrote the following script for example and it's not working. Any ideas on how to fix it ?

local uis = game:GetService('UserInputService')
local plr = game.Players.LocalPlayer
script.Disabled = false

script.Parent.Equipped:connect(function()
script.Disabled = false
uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		print('The tool was equipped')
	end
end)
end)

script.Parent.Unequipped:connect(function()
script.Disabled = true
uis.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		print('The tool was unequipped')
	end
end)
end)

1 comment

1

Why at the beginning do you set the script's Disabled to false? Is it disabled in the first place?

1 answer

Okay firstly.. you can't mess with the script's Disabled property.

I suggest making an a variable true when you equip, and simply checking it upon key press.

local uis = game:GetService('UserInputService')
local plr = game.Players.LocalPlayer
local eq = false --Equipped variable

script.Parent.Equipped:Connect(function() --'connect' is deprecated
	eq = true; --set equipped variable to true
	script.Parent.Unequipped:Wait(); --wait for them to unequip
	eq = false; --set equipped variable to false
end)

uis.InputBegan:Connect(function(input,process)
	if not process then --Make sure they're not chatting
		if input.KeyCode == Enum.KeyCode.E then
			--check equipped val
			print(eq and "The tool is equipped" or "The tool is unequipped");
		end
	end
end)

1 comment

0

Thanks

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