What's wrong with the script?

Hello!

I have a problem. The script is made so that the camera looks only along the line 3, and the script should block clicks W,S And the keys a d do not block. But my script does not completely block W,S.

In the first seconds of the video I press only the keys a, d, and then I press w,s.

Tell me how to fix this.

local player = game:GetService("Players").LocalPlayer
local camera = workspace.CurrentCamera
local contextActionService = game:GetService("ContextActionService")
local userInputService = game:GetService("UserInputService")
local runService = game:GetService("RunService")

contextActionService:UnbindAction("moveForwardAction")
contextActionService:UnbindAction("moveBackwardAction")

local heightOffset = 10 
local distance = 50 

local movementSpeed = 5 
local canMove = true 

camera.CameraType = Enum.CameraType.Scriptable

local function updateCamera()
	if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
		local hrp = player.Character.HumanoidRootPart

		local cameraPosition = Vector3.new(
			hrp.Position.X,
			hrp.Position.Y + heightOffset,
			hrp.Position.Z - distance
		)

		local lookAt = Vector3.new(
			hrp.Position.X,
			hrp.Position.Y,
			hrp.Position.Z - 1
		)

		camera.CFrame = CFrame.new(cameraPosition, lookAt)
	end
end

userInputService.InputBegan:Connect(function(input, gameProcessed)
	if gameProcessed then return end

	if input.KeyCode == Enum.KeyCode.W then
		canMove = true
	elseif input.KeyCode == Enum.KeyCode.S then
		canMove = true
	end
end)

userInputService.InputEnded:Connect(function(input, gameProcessed)
	if gameProcessed then return end

	if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.S then
		canMove = false
	end
end)

runService.RenderStepped:Connect(function()
	if player.Character and player.Character:FindFirstChild("HumanoidRootPart") then
		local hrp = player.Character.HumanoidRootPart

		if canMove then
			if userInputService:IsKeyDown(Enum.KeyCode.W) then
				hrp.CFrame = hrp.CFrame * CFrame.new(0, 0, movementSpeed * runService.RenderStepped:Wait())
			elseif userInputService:IsKeyDown(Enum.KeyCode.S) then
				hrp.CFrame = hrp.CFrame * CFrame.new(0, 0, -movementSpeed * runService.RenderStepped:Wait())
			end
		end

		updateCamera()
	end
end)

2 Likes
if input.KeyCode == Enum.KeyCode.W then
	canMove = true
elseif input.KeyCode == Enum.KeyCode.S then
	canMove = true
end

if canMove then
		if userInputService:IsKeyDown(Enum.KeyCode.W) then
			hrp.CFrame = hrp.CFrame * CFrame.new(0, 0, movementSpeed * runService.RenderStepped:Wait())
		elseif userInputService:IsKeyDown(Enum.KeyCode.S) then
			hrp.CFrame = hrp.CFrame * CFrame.new(0, 0, -movementSpeed * runService.RenderStepped:Wait())
		end
	end
end

Are you sure you didn’t put W and S in place of A and D..

You may be right because it says there true, but it should stand false.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.