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

How do I temporarily stop a script from running?

Asked by 5 years ago
Edited 5 years ago

I am new to Roblox scripting, and I want to temporarily stop a script from running when I press a key.

Code:

01uif = game:GetService("UserInputService")
02 
03 
04uif.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
12end)
13 
14uif.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
20end)

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:

1uif.InputEnded:connect(function(imput)
2    if imput.KeyCode == Enum.KeyCode.C then
3        Script.Disabled = true
4    end
5end)

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.

0
Why don't you just make a variable called disabled and set it to true when you press c (or false if its already true). Then you just need to check to make sure that the variable is false before you let the script do anything. cmgtotalyawesome 1418 — 5y

3 answers

Log in to vote
0
Answered by 5 years ago

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:

01uif = game:GetService("UserInputService");
02local plr = game.Players.LocalPlayer;
03local 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.
04local hum = char:WaitForChild("Humanoid");
05 
06local crouching = false;
07 
08uif.InputBegan:connect(function(imput)
09    if imput.KeyCode == Enum.KeyCode.LeftShift and crouching == false then
10        hum.WalkSpeed = 21
11 
12    end
13end)
14 
15uif.InputEnded:connect(function(imput)
View all 26 lines...

This script will do exactly what you want. There is no need to try to remotely break the input events.

0
At line 25, what event do I add? Another uif.InputEnded:connect(function)? RosyCatCLash 17 — 5y
0
NVM I got it thanks! RosyCatCLash 17 — 5y
0
Sorry, just saw this. Glad you figured it out! Good luck with your game GriffthouBiff 147 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

I spotted something wrong:

1uif.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
5end)

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

0
That is not a big deal anyway. The problem here is when the script disable, that line of code will disable too. Block_manvn 395 — 5y
Log in to vote
0
Answered by 5 years ago

Try replacing that script with this, also be sure to rename that script to "SprintingMechanism" or else it won't work.

01local uis = game:GetService("UserInputService")
02 
03uis.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
09end)
10 
11uis.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
View all 33 lines...

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.

0
First, you can't accept and change the player speed by using server script, it should be local script. Second, you don't need to make a BoolValue in the ReplicatedStorage, it better to do like this SprintToggle = true to debounce. Anyway, it good to see the other beginner like you trying to help someone :D Block_manvn 395 — 5y
0
Thanks AcrylixDev 119 — 5y

Answer this question