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

Why would this resizing fieldofview with for loop won't work as intended?

The camera zooming will only work for first time. Also it will print sprint on clicking shift even if you are not walking.

local player = game.Players.LocalPlayer
local Char = player.Character or player.CharacterAdded:Wait()
local humanoid = script.Parent.Humanoid
local UserInputService = game:GetService("UserInputService")
local Running = false

UserInputService.InputBegan:Connect(function(Input, gameProcessed)
        if UserInputService:IsKeyDown(Enum.KeyCode.LeftShift)and not gameProcessed then
	print("sprint")
		Running = true
		humanoid.WalkSpeed = 32
	end
end)
 
UserInputService.InputEnded:Connect(function(Input, gameProcessed)
	if Input.KeyCode == Enum.KeyCode.LeftShift and not gameProcessed then
		Running = false
		humanoid.WalkSpeed = 16
	end
end)

repeat wait() until Running == true
print("ZOOM")
	for i = 1,5 do
		game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
		wait()
	end
	repeat wait() until Running == false
	print("ZOOMBACK")
	for i = 1,5 do
		game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
		wait()
	end

1 answer

✓ Accepted answer

You can simple put the zoom script inside your script to hold the key for run

Here is fixed script:

local player = game.Players.LocalPlayer
local Char = player.Character or player.CharacterAdded:Wait()
local humanoid = Char:WaitForChild("Humanoid") -- Do not use script.Parent.Humanoid, use Char:WaitForChild("Humanoid")
local UserInputService = game:GetService("UserInputService")
local Running = false

UserInputService.InputBegan:Connect(function(Input, gameProcessed)
	if UserInputService:IsKeyDown(Enum.KeyCode.LeftShift) and not gameProcessed then
        Running = true
        humanoid.WalkSpeed = 32
		-- Added the camera zoom here.
		for i = 1,5 do
			game.Workspace.CurrentCamera.FieldOfView = (70+(i*2))
			wait()
		end
    end
end)

UserInputService.InputEnded:Connect(function(Input, gameProcessed)
	if Input.KeyCode == Enum.KeyCode.LeftShift and not gameProcessed then
		Running = false
		humanoid.WalkSpeed = 16
		-- Added the camera zoom here.
		if Running == false then
			for i = 1,5 do
		        game.Workspace.CurrentCamera.FieldOfView = (80-(i*2))
		        wait()
		    end
		end
	end
end)

-- Removed the repeat wait() until ...

hope it helped :)

Doubts or errors? tell me in the comments

2 comments

0

What do you mean by "you can use workspace"?

0

Forgot,  One person warned me that using "workspace" is deprecated

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