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

How to i get a player to stop shift spring when the player enters a brick?

So my goal is to create a brick when the player walks through he will no longer be able to use my shift to sprint script. The block will be Non collide able. I have the script but I'm not sure what to change in order to make it so the player will be only to walk in the area of the brick.

local mouse = game.Players.LocalPlayer:GetMouse()
local running = false

function getTool()	
	for _, kid in ipairs(script.Parent:GetChildren()) do
		if kid.className == "Tool" then return kid end
	end
	return nil
end


mouse.KeyDown:connect(function (key) 
	key = string.lower(key)
	if string.byte(key) == 48 then
		running = true
		local keyConnection = mouse.KeyUp:connect(function (key)
			if string.byte(key) == 48 then
				running = false
			end
		end)
		for i = 1,5 do
			game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
			wait()
		end
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 30
		repeat wait () until running == false
		keyConnection:disconnect()
		game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
		for i = 1,5 do
			game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
			wait()
		end
	end
end)

Keep in mind I'm fairly new to scripting.

2 answers

✓ Accepted answer

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.

-- Declaration Section 
-- //Game Services 
local UserInputService = game:GetService("UserInputService");
local Workspace = game:GetService("Workspace");

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

-- Declaration Section 
-- //Game Services 
local UserInputService = game:GetService("UserInputService");
local Workspace = game:GetService("Workspace");

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

-- Declaration Section 
-- //Game Services 
local UserInputService = game:GetService("UserInputService");
local Workspace = game:GetService("Workspace");

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

-- Declaration Section 
-- //Game Services 
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");

-- //Variables 
local LeftShift = Enum.KeyCode.LeftShift;
local isPlayerRunning = false;

-- [Processing Section]

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

-- [Connecting Section]
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.

-- Declaration Section 
-- //Game Services 
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");

-- //Brick Location 
local TouchBrick = Workspace:FindFirstChild("TouchBrick"); --Change to your actual name. 

-- //Variables 
local LeftShift = Enum.KeyCode.LeftShift;
local isPlayerRunning = false;

-- [Processing Section]

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;

-- [Connecting Section]
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.

You will need to modify the sprint script in order to disable when colliding with the part. As I do not have information for what part you want to trigger this, I am not able to insert that part into your script. Basically, :Connect() to when the part is collided with a player and set running to false. It looks like your script already handles that fairly good. After that, you will need to include another variable named accordingly that your KeyDown handler checks and returns if that blocking variable is true.

I hope this helps. I haven't played Roblox in forever and came temporarily for a little bit of help on a tiny project someone asked me to do.

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