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

Attempt to index nil with 'FindFirstChild'?

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)

6 comments

1

character is nil

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.

0

^^^ local char = player.Character or player.CharacterAdded:Wait()

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?

0

Exactly

0

So we play the waiting game

1 answer

✓ Accepted answer

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()

2 comments

0

Nice you literally converted our comments into an answer

0

I didn't even see your comment when I wrote this.

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