Attempt to index nil with body

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I wanna make script to car interiors
  2. What is the issue? Include screenshots / videos if possible!
    image

    signal script(not interior one)

    interior one script(not working)
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Tried to fix script by my own but i couldn’t fix it
    After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!
-- This is an example Lua code block

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.



image

Please use the code blocks that are in the support template instead of taking screenshots of your code, it’s extremely hard to read.

You’ve posted a lot of code and I don’t think your issue is even clear, let alone where in the code the issue is.

3 Likes

Hi!

Please don’t just paste all your scripts, hoping someone will solve it for you. And it’s better to paste any scripts as text, with lua markup. Also the full error message would be helpful.

That said, at the start of the script you make a variable car, which points to the value of an objectValue in the same location as the script.

Later in the script, we try to look into that car variable to find car.Body, but you say you get the error message ‘attempt to index nil with body’. That means that the car variable is empty (nil).

So most likely, either the object value is not set, and no longer pointing to a car. Or perhaps alternatively, the car is destroyed at some point, after which it can no longer be found.

Good luck!

2 Likes

Here is the error screenshot



Only Line 38 Code:
workspace:FindFirstChild(Player.Name.."'s Car").Body.Lights.Ri.arrow.Color = BrickColor.new("New Yeller")

Full Code:

local ri = script.Parent.Signal.RI.Value
local li = script.Parent.Signal.LI.Value
local hi = script.Parent.Signal.HI.Value
local Player = script.Parent.Parent.Parent.Parent
while (true) do
	wait(0.5)
print("I am working")

while (ri == true) do
		--script.Parent.Parent.Parent.Body.Lights.Ri.arrow.Color = BrickColor.new("Lime green")
		workspace:FindFirstChild(Player.Name.."'s Car").Body.Lights.Ri.arrow.Color = BrickColor.new("Lime green")
	wait(1)
		--script.Parent.Parent.Parent.Body.Lights.Ri.arrow.Color = BrickColor.new("New Yeller")
		workspace:FindFirstChild(Player.Name.."'s Car").Body.Lights.Ri.arrow.Color = BrickColor.new("New Yeller")
end

while (li == true) do
		--script.Parent.Parent.Parent.Body.Lights.Li.arrow.Color = BrickColor.new("Lime green")
		workspace:FindFirstChild(Player.Name.."'s Car").Body.Lights.Li.arrow.Color = BrickColor.new("Lime green")
	wait(1)
		--script.Parent.Parent.Parent.Body.Lights.Li.arrow.Color = BrickColor.new("New Yeller")
		workspace:FindFirstChild(Player.Name.."'s Car").Body.Lights.Li.arrow.Color = BrickColor.new("New Yeller")
end

while (hi == true) do
		--script.Parent.Parent.Parent.Body.Lights.Ri.arrow.Color = BrickColor.new("Lime green")
		--script.Parent.Parent.Parent.Body.Lights.Li.arrow.Color = BrickColor.new("Lime green")
		workspace:FindFirstChild(Player.Name.."'s Car").Body.Lights.Ri.arrow.Color = BrickColor.new("Lime green")
		workspace:FindFirstChild(Player.Name.."'s Car").Body.Lights.Li.arrow.Color = BrickColor.new("Lime green")
	wait(1)
		--script.Parent.Parent.Parent.Body.Lights.Ri.arrow.Color = BrickColor.new("New Yeller")
		--script.Parent.Parent.Parent.Body.Lights.Li.arrow.Color = BrickColor.new("New Yeller")
		workspace:FindFirstChild(Player.Name.."'s Car").Body.Lights.Ri.arrow.Color = BrickColor.new("New Yeller")
		workspace:FindFirstChild(Player.Name.."'s Car").Body.Lights.Li.arrow.Color = BrickColor.new("New Yeller")
	end
	--script.Parent.Parent.Parent.Body.Lights.Ri.arrow.Color = BrickColor.new("New Yeller")
	--script.Parent.Parent.Parent.Body.Lights.Li.arrow.Color = BrickColor.new("New Yeller")
	workspace:FindFirstChild(Player.Name.."'s Car").Body.Lights.Ri.arrow.Color = BrickColor.new("New Yeller")
	workspace:FindFirstChild(Player.Name.."'s Car").Body.Lights.Li.arrow.Color = BrickColor.new("New Yeller")
end

Oh, lol… Well in the Signal localscript the reference script.Parent.Car.Value should crash, I guess, as that item is not there (looking at explorer). I’m not sure why it doesn’t if all this is put in playerGui.

I’m surprised the other script even works in playerGui, but it seems that:

workspace:FindFirstChild(Player.Name.."'s Car")

Does not actually find any car in workspace. Getting the player reference using parent.parent.parent etc is not the cleanest way, maybe something went wrong there?

Test it by printing Player.Name. Then look if there is a car “(playername)'s Car” in workspace that it should have found. Probably (/certainly) there isn’t, which is why FindFirstChild returns nil, and then trying to access .Body gives this crash.

Edit: In fact if we compare

script.Parent.Parent.Parent.Parent

with your error message and count up, then we see that it is in fact not the player (but the parent of the player). I’ll leave the rest up to you :slight_smile: