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.
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.
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
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.