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

How do I temporarily stop a script from running?

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

Code:

uif = game:GetService("UserInputService")


uif.InputBegan:connect(function(imput)
	if imput.KeyCode == Enum.KeyCode.LeftShift then
		
		local plr = game.Players.LocalPlayer
		local char = script.Parent.Humanoid
		char.WalkSpeed = 21
	
	end
end)

uif.InputEnded:connect(function(imput)
	if imput.KeyCode == Enum.KeyCode.LeftShift then
		local plr = game.Players.LocalPlayer
		local char = script.Parent.Humanoid
		char.WalkSpeed = 16
	end
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:

uif.InputEnded:connect(function(imput)
	if imput.KeyCode == Enum.KeyCode.C then
		Script.Disabled = true
	end
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.

1 comment

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.

3 answers

✓ Accepted answer

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:

uif = game:GetService("UserInputService");
local plr = game.Players.LocalPlayer;
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.
local hum = char:WaitForChild("Humanoid");

local crouching = false;

uif.InputBegan:connect(function(imput)
    if imput.KeyCode == Enum.KeyCode.LeftShift and crouching == false then
        hum.WalkSpeed = 21

    end
end)

uif.InputEnded:connect(function(imput)
    if imput.KeyCode == Enum.KeyCode.LeftShift and crouching == false then
        hum.WalkSpeed = 16
    end
end)

uif.InputEnded:connect(function(imput)
    if imput.KeyCode == Enum.KeyCode.C then
	crouching = true;
        hum.WalkSpeed = --wanted crouching speed
    end--add another event for when they finish crouching to set crouching to false.
end)

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

3 comments

0

At line 25, what event do I add? Another uif.InputEnded:connect(function)?

0

NVM I got it thanks!

0

Sorry, just saw this. Glad you figured it out! Good luck with your game

I spotted something wrong:

uif.InputEnded:connect(function(imput)
    if imput.KeyCode == Enum.KeyCode.C then
        Script.Disabled = true -- I spotted that "Script" should have a lower case s: "script"
    end
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

1 comment

0

That is not a big deal anyway. The problem here is when the script disable, that line of code will disable too.

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

local uis = game:GetService("UserInputService")

uis.InputBegan:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		local plr = game.Players.LocalPlayer
		local char = script.Parent.Humanoid
		char.Walkspeed = 21
	end
end)

uis.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftShift then
		local plr = game.Players.LocalPlayer
		local char = script.Parent.Humanoid
		char.Walkspeed = 16
	end
end)

-- Edited Part
local replicatedstorage = game:GetService("ReplicatedStorage")
local sprinttoggle = Instance.new("BoolValue", replicatedstorage)
sprinttoggle.Name = "sprinttoggle"
sprinttoggle.Value = true


uis.InputEnded:connect(function(input)
	if input.KeyCode == Enum.KeyCode.C then
		if sprinttoggle.Value == true then
			sprinttoggle.Value = false
			script.Disabled = true
		end
	end
end)

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.

2 comments

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

0

Thanks

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