I have created a game above the ground in which there is a baseplate at the bottom.
I want this baseplate to be wherever the player is, but at the same height
I have created the following script:
local basePlate = workspace.BP
while true do
wait()
basePlate.CFrame = CFrame.new(game.Players.LocalPlayer:FindFirstChild("Character"):FindFirstChild("HumanoidRootPart").CFrame.X, basePlate.CFrame.Y, basePlate.CFrame.Z)
end
but i keep getting the error:
Workspace.KieranKreates.BasePlate:5: attempt to index nip with 'FindFirstChild'
Line 5
When you get an error saying “attempt to index nil with X”, it means whatever you’re using X on does not exist, or at least it doesn’t exist when the code runs. In this specific case, the Character and HumanoidRootPart are bound to exist at some point, but it’s highly likely that they will fail to load in before the script runs.
In short, the root part is being searched for before the character is even found. :FindFirstChild(“Character”) actually does not work because .Character is a property instead of a descendant. If this is confusing to you, you can view the properties of a Player here for a better understanding.
On that same page, you will see the solution to your issue. .CharacterAdded is an event which fires when the character is assigned. You can use a line like this:
local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
This will search for the character first, and if it doesn’t exist, it will then wait for it to load in.
local baseplate = workspace.Baseplate
local player = game.Players.LocalPlayer
repeat task.wait() until player.Character
local char = player.Character
local hrp = char.HumanoidRootPart
while task.wait() do
baseplate.Position = Vector3.new(hrp.Position.X, baseplate.Position.Y, hrp.Position.Z)
end
“LocalPlayer” is not defined in server scripts. You’ll need to change the script to a local script & place it inside the StarterPlayerScripts or the StarterCharacterScripts folder.
You can, using FindFirstChild yields until it finds the child, it doesn’t just run all at once, so it will know the instance to find the child of.
Who ever said this was a server script? The error is talking about FindFirstChild not finding Character because its a property, not that it can’t find LocalPlayer. Plus, its already been solved.
local basePlate = workspace.BP
while true do
wait()
basePlate.CFrame = CFrame.new(game.Players.LocalPlayer.Character:FindFirstChild("HumanoidRootPart").CFrame.X, basePlate.CFrame.Y, basePlate.CFrame.Z)
end