Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Attempt to index nil with 'FindFirstChild'?

Asked by 3 years ago

I'm trying to create a hold shift to sprint script. But when I try to get the player's character it gives the error that's in the title. Here's the code:

--Local script
local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local char = player.Character
local hum = player.Character:FindFirstChild("Humanoid")
local held = false


UIS.InputBegan:Connect(function(input, gameProcessedEvent)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        held = true
    end
    while held == true do
        wait()
        hum.WalkSpeed = 60
    end
end)

UIS.InputEnded:Connect(function(input, gameProcessedEvent)
    if input.KeyCode == Enum.KeyCode.LeftShift then
        held = false
    end
end)
1
character is nil JesseSong 3916 — 3y
1
The script is running before the character loads in. You need to do player.CharacterAdded:Wait() , and it will wait for the character to load in then finish the script. 2b2tpvp 17 — 3y
0
^^^ local char = player.Character or player.CharacterAdded:Wait() User#30567 0 — 3y
0
so you guys are saying that the script is trying to check if the player's character is there so fast that it ran before they got to load in? ack_superbear 35 — 3y
View all comments (2 more)
0
Exactly User#30567 0 — 3y
0
So we play the waiting game User#30567 0 — 3y

1 answer

Log in to vote
1
Answered by 3 years ago

You need to wait for the character to load in first.

Replace

local char = player.Character

with

local char = player.Character or player.CharacterAdded:Wait()
0
Nice you literally converted our comments into an answer User#30567 0 — 3y
0
I didn't even see your comment when I wrote this. lightifieds 40 — 3y
Ad

Answer this question