I am new to Roblox scripting, and I want to temporarily stop a script from running when I press a key.
Code:
01 | uif = game:GetService( "UserInputService" ) |
02 |
03 |
04 | uif.InputBegan:connect( function (imput) |
05 | if imput.KeyCode = = Enum.KeyCode.LeftShift then |
06 |
07 | local plr = game.Players.LocalPlayer |
08 | local char = script.Parent.Humanoid |
09 | char.WalkSpeed = 21 |
10 |
11 | end |
12 | end ) |
13 |
14 | uif.InputEnded:connect( function (imput) |
15 | if imput.KeyCode = = Enum.KeyCode.LeftShift then |
16 | local plr = game.Players.LocalPlayer |
17 | local char = script.Parent.Humanoid |
18 | char.WalkSpeed = 16 |
19 | end |
20 | end ) |
This script works as a shift run code, and I want to stop this code from running when I press the key, 'c'.
I tried this way from stopping it:
1 | uif.InputEnded:connect( function (imput) |
2 | if imput.KeyCode = = Enum.KeyCode.C then |
3 | Script.Disabled = true |
4 | end |
5 | end ) |
However, the problem is that when I don't press c, the script doesn't work and it stops functioning completely. I tried to put a 'Script.Disabled = false' at the beginning of script, but that doesn't work as well.
Can someone please tell me how I fix this problem so that when I press c, the shift run script doesn't function.
Thank you, this issue has been bugging me for quite a while.
You are not able to remotely break the script like what you want to do, however, you can achieve exactly what you said you want by simply nulling out what the other part of the script does in the cancelling part. Example:
01 | uif = game:GetService( "UserInputService" ); |
02 | local plr = game.Players.LocalPlayer; |
03 | local char = plr.Character; --redefining the character and player every function is very unnecessary. Furthermore, it's far safer to reference the character from the player than any other way. |
04 | local hum = char:WaitForChild( "Humanoid" ); |
05 |
06 | local crouching = false ; |
07 |
08 | uif.InputBegan:connect( function (imput) |
09 | if imput.KeyCode = = Enum.KeyCode.LeftShift and crouching = = false then |
10 | hum.WalkSpeed = 21 |
11 |
12 | end |
13 | end ) |
14 |
15 | uif.InputEnded:connect( function (imput) |
This script will do exactly what you want. There is no need to try to remotely break the input events.
I spotted something wrong:
1 | uif.InputEnded:connect( function (imput) |
2 | if imput.KeyCode = = Enum.KeyCode.C then |
3 | Script.Disabled = true -- I spotted that "Script" should have a lower case s: "script" |
4 | end |
5 | end ) |
This may just be a typo and not the problem but if this worked for you please accept this as your answer.
All the best,
PrismaticFruits - obviously a very talented scripter
Try replacing that script with this, also be sure to rename that script to "SprintingMechanism" or else it won't work.
01 | local uis = game:GetService( "UserInputService" ) |
02 |
03 | uis.InputBegan:connect( function (input) |
04 | if input.KeyCode = = Enum.KeyCode.LeftShift then |
05 | local plr = game.Players.LocalPlayer |
06 | local char = script.Parent.Humanoid |
07 | char.Walkspeed = 21 |
08 | end |
09 | end ) |
10 |
11 | uis.InputEnded:connect( function (input) |
12 | if input.KeyCode = = Enum.KeyCode.LeftShift then |
13 | local plr = game.Players.LocalPlayer |
14 | local char = script.Parent.Humanoid |
15 | char.Walkspeed = 16 |
After that, insert a script into serverscriptservice named "SprintingServerFire". If you know what you are doing, you can adjust it accordinlgly and it should work.