local mouse = game.Players.LocalPlayer:GetMouse()
local uis = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local db = false
local dbtime = 3
--script.Parent.Activated:Connect(function()
--game.ReplicatedStorage.Events.KnifeThrow:FireServer(mouse.Hit.Position)
--end)
uis.InputBegan:Connect(function(Input, IsTyping)
if not IsTyping then
if not db then
db = true
print("key clicked!")
if Input.KeyCode == Enum.KeyCode.E and script.Parent.Equipped == true then
game.ReplicatedStorage.Events.KnifeThrow:FireServer(mouse.Hit.Position)
end
wait(dbtime)
db = false
end
end
end)
I want to know why the key clicked isn’t printing when I am pressing the E key.
script.Parent.Equipped == true is probably the culprit. Equipped is an event, and you’re comparing it to the boolean true, and this will never be true.
You should use ContextActionService:BindAction instead of InputBegan. Also, it will help if you use the debugger or add other print statements to ensure your code is getting to where you expect it to.
local mouse = game.Players.LocalPlayer:GetMouse()
local uis = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local db = false
local dbtime = 3
local equipped = false
uis.InputBegan:Connect(function(Input, IsTyping)
if not IsTyping then
if not db then
db = true
print("key clicked!")
if Input.KeyCode == Enum.KeyCode.E and equipped == true then
game.ReplicatedStorage.Events.KnifeThrow:FireServer(mouse.Hit.Position)
end
wait(dbtime)
db = false
end
end
end)
script.Parent.Equipped:Connect(function() equipped = true end)
script.Parent.Unequipped:Connect(function() equipped = false end)
local CAS = game:GetService("ContextActionService")
local function handleAction(actionName, inputState, inputObject)
if actionName == "KnifeThrow" then
if inputState == Enum.InputState.Begin then
-- do something
end
end
end
CAS:BindAction("KnifeThrow", handleAction, false, Enum.KeyCode.E)
CAS is all about, well, contextual actions. When you equip a tool you enter a context in which you can perform an action. Upon entering that context, you call BindAction and provide it:
(string) the name of that action,
a function which handles the input of that action,
(boolean) whether a button is created for players using touch input, and
When you unequip the tool, you leave the context and you call UnbindAction, providing it the action name you provided BindAction (1 above).
So, in your case:
local ContextActionService = game:GetService("ContextActionService")
local tool = script.Parent
local ACTION_THROW_KNIFE = "Throw Knife"
local INPUT_THROW_KNIFE = Enum.UserInputType.MouseButton1
local function throwAKnife()
-- Whatever logic your game needs to perform this action goes here! Yours was:
--game.ReplicatedStorage.Events.KnifeThrow:FireServer(mouse.Hit.Position)
end
local function handleAction(actionName, inputState, inputObject)
if actionName == ACTION_THROW_KNIFE then
if inputState == Enum.UserInputState.Begin then
throwAKnife()
end
end
end
tool.Equipped:Connect(function ()
-- We've entered a context wherein the player can throw knives! Bind:
ContextActionService:BindAction(
ACTION_THROW_KNIFE, -- the action name
handleAction, -- function to process the action
false, -- whether a button is created for touch input players
INPUT_THROW_KNIFE -- inputs which trigger this action
)
end)
tool.Unequipped:Connect(function ()
-- We're leaving the context that allows for knife-throwing, so unbind
ContextActionService:UnbindAction(ACTION_THROW_KNIFE)
end)
You might think this is long-winded and you’re partially right. However, this makes it very straightforward to support cross-platform play, modify behaviors of existing actions, and support multiple actions per action handler function. For example, you could add a charge-up time by measuring the time between Enum.UserInputState.Begin and End (eg how long they held the button down for).
local UIS = game:GetService("UserInputService")
local function handleAction(actionName, inputState, inputObject)
if actionName == "KnifeThrow" and not UIS:GetFocusedTextBox() then
if inputState == Enum.InputState.Begin then
-- do something
end
end
end