KeyDown is depreciated and should not be used anymore
I will be re-changing your script from top to bottom. I will explain all the parts.
UserInputService is the new way of getting a Keyboard or a Mouse Input. Lets first implement that and declare some other variables.
local UserInputService = game:GetService("UserInputService");
local Workspace = game:GetService("Workspace");
local LeftShift = Enum.KeyCode.LeftShift;
Here, we are getting UserInputService and Workspace from the game. Then we declared LeftShift as a variable holding the KeyCode, LeftShift.
Now we are going to work when a player actually presses the LeftShift.
local UserInputService = game:GetService("UserInputService");
local Workspace = game:GetService("Workspace");
local LeftShift = Enum.KeyCode.LeftShift;
local WhenPressedLeftShift = function (Input, GameProccessedEvent)
if Input.KeyCode == LeftShift then
print("Left Shift was pressed");
end
end;
In the following code, we are checking if the Player actually pressed LeftShift or not. If they did, it will print Left Shift was pressed and if not then nothing happens.
Now we want to make the Player actually run fast.
local UserInputService = game:GetService("UserInputService");
local Workspace = game:GetService("Workspace");
local LeftShift = Enum.KeyCode.LeftShift;
local isPlayerRunning = false;
local WhenPressedLeftShift = function (Input, GameProccessedEvent)
if Input.KeyCode == LeftShift then
print("Left Shift was pressed");
if isPlayerRunning == false then
isPlayerRunning = true;
for i = 1,5 do
Workspace.CurrentCamera.FieldOfView = (70+(i*2));
wait();
end;
else
isPlayerRunning = false;
for i = 1,5 do
Workspace.CurrentCamera.FieldOfView = (80-(i*2));
wait();
end;
end;
end;
end;
UserInputService.InputBegan:Connect(WhenPressedLeftShift)
You will notice that It works awkwardly. Fear not, here is a simple fix to that.
local UserInputService = game:GetService("UserInputService");
local Workspace = game:GetService("Workspace");
local Camera = Workspace.CurrentCamera;
local Player = game:GetService("Players").LocalPlayer;
local Character = Player.Character or Player.CharacterAdded:Wait();
local Humanoid = Character:WaitForChild("Humanoid");
local LeftShift = Enum.KeyCode.LeftShift;
local isPlayerRunning = false;
local WhenPressedLeftShift = function (Input, GameProccessedEvent)
if Input.KeyCode == LeftShift then
print("Left Shift was pressed");
if isPlayerRunning == false then
isPlayerRunning = true;
for i = 1,5 do
Camera.FieldOfView = (70+(i*2));
wait();
end;
Humanoid.WalkSpeed = 30
end;
end;
end;
local WhenUnPressedLeftShift = function (Input, GameProcessedEvent)
if Input.KeyCode == LeftShift then
print("Left Shift was ended");
if isPlayerRunning == true then
isPlayerRunning = false;
for i = 1,5 do
Camera.FieldOfView = (80-(i*2));
wait();
end;
Humanoid.WalkSpeed = 16;
end
end
end
UserInputService.InputBegan:Connect(WhenPressedLeftShift);
UserInputService.InputEnded:Connect(WhenUnPressedLeftShift);
The above is the fully working Speed to Shift script.
Now, you wanted it to stop working when touching a brick. For this, we will have to use the Touched Event.
local UserInputService = game:GetService("UserInputService");
local Workspace = game:GetService("Workspace");
local Camera = Workspace.CurrentCamera;
local Player = game:GetService("Players").LocalPlayer;
local Character = Player.Character or Player.CharacterAdded:Wait();
local Humanoid = Character:WaitForChild("Humanoid");
local TouchBrick = Workspace:FindFirstChild("TouchBrick");
local LeftShift = Enum.KeyCode.LeftShift;
local isPlayerRunning = false;
local WhenPressedLeftShift = function (Input, GameProccessedEvent)
if Input.KeyCode == LeftShift then
print("Left Shift was pressed");
if isPlayerRunning == false then
isPlayerRunning = true;
for i = 1,5 do
Camera.FieldOfView = (70+(i*2));
wait();
end;
Humanoid.WalkSpeed = 30
end;
end;
end;
local WhenUnPressedLeftShift = function (Input, GameProcessedEvent)
if Input.KeyCode == LeftShift then
print("Left Shift was ended");
if isPlayerRunning == true then
isPlayerRunning = false;
for i = 1,5 do
Camera.FieldOfView = (80-(i*2));
wait();
end;
Humanoid.WalkSpeed = 16;
end
end
end
local DeleteScriptWhenTouched = function (Hit)
if Hit.Parent:FindFirstChild("Humanoid") then
for i = 1,5 do
Camera.FieldOfView = (80-(i*2));
wait();
end;
Humanoid.WalkSpeed = 16;
script:Destroy();
end;
end;
UserInputService.InputBegan:Connect(WhenPressedLeftShift);
UserInputService.InputEnded:Connect(WhenUnPressedLeftShift);
TouchBrick.Touched:Connect(DeleteScriptWhenTouched);
The above is your final working code. Should you have any questions, comment below, and I will fix it or explain it to you.
Have a lovely day of coding.