Character Not Being Found When Added?

Can someone help me? Trying to make it to where I can run when i press ctrl, which I think I did. But at the beginning where I try to find the character, I think I did wrong. What’s a fix for this?

The output for it is: Workspace.Script:2: attempt to index nil with 'Character’

local player = game.Players.LocalPlayer
local character = player.Character:WaitForChild("Humanoid")

local speed = 100 -- Set the speed increase here

local function onKeyPress(inputObject)
    if inputObject.KeyCode == Enum.KeyCode.LeftControl then
        if character then
            local humanoid = character:WaitForChild("Humanoid")
            humanoid.WalkSpeed = humanoid.WalkSpeed + speed
        end
    end
end

local function onKeyRelease(inputObject)
    if inputObject.KeyCode == Enum.KeyCode.LeftControl then
        if character then
            local humanoid = character:WaitForChild("Humanoid")
            humanoid.WalkSpeed = humanoid.WalkSpeed - speed
        end
    end
end

player.KeyDown:Connect(onKeyPress)
player.KeyUp:Connect(onKeyRelease)

try putting wait() at the first line.

wait()
local player = game.Players.LocalPlayer
local character = player.Character:WaitForChild("Humanoid")

local speed = 100 -- Set the speed increase here

local function onKeyPress(inputObject)
    if inputObject.KeyCode == Enum.KeyCode.LeftControl then
        if character then
            local humanoid = character:WaitForChild("Humanoid")
            humanoid.WalkSpeed = humanoid.WalkSpeed + speed
        end
    end
end

local function onKeyRelease(inputObject)
    if inputObject.KeyCode == Enum.KeyCode.LeftControl then
        if character then
            local humanoid = character:WaitForChild("Humanoid")
            humanoid.WalkSpeed = humanoid.WalkSpeed - speed
        end
    end
end

player.KeyDown:Connect(onKeyPress)
player.KeyUp:Connect(onKeyRelease)

You can use UserInputService to do this

--Script in StarterPlayer
local Player = game:GetService("Players").LocalPlayer
local UIS = game:GetService("UserInputService")
local Speed = 100 --Set the speed increase here

UIS.InputBegan:Connect(function(Input, Event)
	local Character = Player.Character
	if Input.KeyCode == Enum.KeyCode.LeftControl and Character then
		Character.Humanoid.WalkSpeed += Speed
	end
end)

UIS.InputEnded:Connect(function(Input, Event)
	local Character = Player.Character
	if Input.KeyCode == Enum.KeyCode.LeftControl and Character then
		Character.Humanoid.WalkSpeed -= Speed
	end
end)
local players = game:GetService("Players")

local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")