How to take out a knife (not a tool)

  1. What do you want to achieve? I want to make a code where if you press a keycode (example: V) you will take out the knife and put it back in after a few seconds just like call of duty.

  2. What is the issue? the problem is, I don’t know where to start and I don’t know how to create a code that take out a knife,

  3. What solutions have you tried so far? I tried to find an example on youtube but there is none, I didn’t check it in the developer hub because I don’t know the code to pull out a knife.

all I want is how to create a code to pull out a knife, I don’t need any other code.

1 Like

Ok, you said you don’t know where to start - so let’s break it down into steps. So firstly we want to detect when a keycode is pressed. You can do that using ContextActionService or UserInputService. Though I’d probably recommend ContextActionService.
Step 1) Use ContextActionService to detect when the player presses a key.

Next, we want to ‘take out the knife’. What exactly does it mean to take out the knife? Do you mean you want the player to be holding a knife and then play an animation? In that case, you’d need to
Step 2) Have the script weld a model or a part (in this case, the knife) to the player’s hand
Step 3) Then have the player’s character play an animation
Step 4) Wait for the animation to finish.
Step 5) Destroy the model/part.

Search online for how to do each specific step like ‘How to use ContextActionService’ or ‘How to play an animation on a character’ as opposed to the entire thing you want to create, and you might have more luck finding tutorials and videos to help you create your game. Hope this helped somewhat.

2 Likes

Just a question, as I do alot of my work through userinputservice. Why Contextaction?

Hi! The main reason I recommend ContextActionService in this particular situation is because of this: Say, the button to ‘take out the knife’ was the keycode ‘V’. Using ContextActionService, if the player types ‘V’ whilst in chat, or to try and use a proximity prompt, then they would not ‘take out the knife’. They would only ‘take out the knife’ if they pressed ‘V’ and not in chat nor near the proximity prompt.

Using UserInputService, if you typed ‘V’ in chat or to try and use a proximity prompt, then you would also activate ‘take out the knife’ at the same time. I assumed that Lynx wouldn’t want players to constantly ‘take out the knife’ whenever they typed ‘V’ in chat.

Some more reasons:

  • You can very easily add additional buttons to the same action (for example to add controller support or mobile support).
  • You can easily bind or unbind the action depending on if the character has a knife equipped.

I’ll admit I don’t have a lot of experience using UserInputService, and i’m sure you can achieve a similar result with a bit more code, so I made sure to mention it just in case they want to check it out.

1 Like

exactly, I want the player to hold the knife and play the animation (like swinging the knife), but I have one more question, does the “ContextActionService” work forever? like you can ‘take out the knife’ whenever you want to

Yes, ContextActionService will work for as long as you want it to. If you always want players to be able to ‘take out the knife’ whenever they want, you can do that. And if you ever wanted to stop players from being able to ‘take out the knife’, you can do that too.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.