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

What script will make this shoot when hitting the key?

1 answer

✓ Accepted answer

Hey DarkAssasin0,

One event you can use for detecting keys being pressed on the keyboard is the InputBegan event for UserInputService. Here is the wiki page for that and here is an example of it:

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.

##Btw, I just figured out that for the tool events above to work, you must have a part under the tool named "Handle". Well, I gave you the basic ideas of how you can do what you want. I'll leave the rest to your creativity and I hope you have a great day/night. The links for the methods I used in the above scripts will be below.

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

3 comments

0

You waste your time on nonconstructive questions.

0

RUDE

0

Thanks so much! I'm a bit new to scripting and don't fully understand that, but I'll learn ;D

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