How can I get it so that when the player hits 'q' with the tool equipped, it shoots a ball that triggers and explosion?
Hey DarkAssasin0,
local uis = game:GetService("UserInputService") -- Declares a variable for the service "UserInputService" local player = game.Players.LocalPlayer -- Declares a variable for the player, since this is in a local script, this is how you can get the player. uis.InputBegan:Connect(function(obj, gp) -- Anonymous function with the parameters 'obj and gp'. obj is what the user presses on the keyboard and gp is used to check if the key was meant to be pressed.(If it's while the user is typing then, the key getting pressed won't activate this function from the below if statement. if obj.KeyCode == Enum.KeyCode.Q and not gp then -- Checks if the key pressed was 'Q' and checks if the key was pressed while the user was trying to type in chat or, just pressed. If it was pressed while trying to type in chat then this if statement won't allow the rest of the function to run. print("Q was pressed.") -- Prints "Q was pressed." end -- end for the if statement end) -- end for the anonymous function. Make sure the ')' is there at the end because, that is how anonymous functions are ended.
The above script will show you how to make the function activate if the user presses down 'Q' and here I will show you how to check if the user has the tool equipped or not.
local equipped = false -- Declares a variable for a boolean value and sets it to false. local tool = script.Parent -- Declares a variable for the tool itself. This is a local script btw. tool.Equipped:Connect(function() -- Anonymous function declaration for the Equipped Event. equipped = true; -- Sets boolean to true. This means that the tool has been equipped. end) -- End for anonymous function. tool.Unequipped:Connect(function() -- Anonymous function declaration for the Unequipped Event. equipped = false; -- Sets boolean to false. This means that the tool is no longer equipped. end) -- End for the anonymous function.
Here is the anonymous function explanation.
Here is the booleans explanation.
Here are all the events of the tool.
Here is the local player explanation.
~~ KingLoneCat