Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Attempt to index nil with 'FindFirstChild'?

Asked by 7 days 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 3372 — 7d
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 — 7d
0
^^^ local char = player.Character or player.CharacterAdded:Wait() TTChaos 731 — 7d
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 33 — 7d
View all comments (2 more)
0
Exactly TTChaos 731 — 7d
0
So we play the waiting game TTChaos 731 — 7d

1 answer

Log in to vote
1
Answered by 7 days 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 TTChaos 731 — 7d
0
I didn't even see your comment when I wrote this. lightifieds 25 — 2d
Ad

Answer this question