Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make something appear when a key is held down and the player is running?

Asked by 5 years ago

I'm making a game and one part is if you are moving/running and you press left shift an aura will appear and you'll go faster, like in Sonic games. I tried one script, but when I used it it rarely worked. Sometimes I would let go of left shift and there would be a long delay before the aura went away. This script didn't include the part where you have to be running though. The aura also had particles on it and I made it so they would appear with the aura when left shift was pressed, but they wouldn't appear. I'm using this for a StarterCharacter so the script is a local script in StarterCharacterScripts. If anyone knows how to do this so it works all the time then I would appreciate it!

Thanks.

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

Hello Ben. I think I found the solution to your problem.

What we need to do is create a LocalScript(so we can access the LocalPlayer) and place it into our StarterPack, as it's the pack we start off with when we join.

Now you need to put some variables into that script.

1local plr = game.Players.LocalPlayer -- defines player
2local char = plr.Character or plr.CharacterAdded:Wait() -- defines character
3local uis = game:GetService("UserInputService") -- shorter defintion
4local walk = 16 -- our walk value
5local sprint = 32 -- our running/sprinting value

Now that we have sorted all of that out, it's time to begin functioning.

We're going to firstly call some functions(InputBegan and InputEnded)

1uis.InputBegan:Connect(function(key)
2 
3end)
4 
5uis.InputEnded:Connect(function(key)
6 
7end)

Then we're going to check if whoever is clicking the button is clicking LShift(Left Shift)

01uis.InputBegan:Connect(function(key)
02    if key.KeyCode == Enum.KeyCode.LeftShift then
03 
04    end
05end)
06 
07uis.InputEnded:Connect(function(key)
08    if key.KeyCode == Enum.KeyCode.LeftShift then
09 
10    end
11end)

Now that we now know what button they are pressing, we're going to run some code if they are pressing that button.

In this case we'll be using our 'walk' and 'sprint' value.

01uis.InputBegan:Connect(function(key)
02    if key.KeyCode == Enum.KeyCode.LeftShift then
03        char.Humanoid.WalkSpeed = sprint
04    end
05end)
06 
07uis.InputEnded:Connect(function(key)
08    if key.KeyCode == Enum.KeyCode.LeftShift then
09        char.Humanoid.WalkSpeed = walk
10    end
11end)

Tadahh!!! All you have to do is now put the variables we've listed from above into the script and then you're officially done! You can insert Aura's if you wish through the Server to the Client, or the Server in general. If you do not know how to do that, check out videos specialising in the topic, 'RemoteEvents'.

01local plr = game.Players.LocalPlayer
02local char = plr.Character or plr.CharacterAdded:Wait()
03local uis = game:GetService("UserInputService")
04local walk = 16
05local sprint = 32
06 
07uis.InputBegan:Connect(function(key)
08    if key.KeyCode == Enum.KeyCode.LeftShift then
09        char.Humanoid.WalkSpeed = sprint
10    end
11end)
12 
13uis.InputEnded:Connect(function(key)
14    if key.KeyCode == Enum.KeyCode.LeftShift then
15        char.Humanoid.WalkSpeed = walk
16    end
17end)
I hope I helped, if I did please click 'Accept' to show me support!
0
Well it worked, sort of. The aura DOES appear when I press left shift, but I only want it to appear when the character is running. That is an issue I have been actually been having with a few other things. Do you know how to do that? benawesomeness8954 0 — 5y
0
Well in the InputBegan function - make it so the Particles/Aura appears using Enabled or Visible. Then in InputEnded - make it so the Particles/Aura disappears. TheOnlySmarts 233 — 5y
Ad

Answer this question