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

How to Fix attempt to index local 'bodyVel' (a nil value) ?

I am trying to get my spawn in my game working but as soon as I click the button to get teleported and the countdown reaches 0 I get this error and don't get teleported to this new location in the map.

repeat wait() until Player.Character:FindFirstChild("Torso")
		Player.Character.Torso.Anchored = true
		Player.Character.Torso.CFrame = CFrame.new(Vector3.new(-21.3, 15019, -44.1));--teleobject.CFrame
		local bodyVel = Player.Character.Torso:FindFirstChild("BodyAnchor")
		bodyVel.maxForce = Vector3.new(5000000,5000000,5000000)
		bodyVel.D = 1000
		bodyVel.P = 10000
		bodyVel.position = Workspace.Scenery:WaitForChild("ReturnPad").Position
		Player.Character.Torso.Anchored = false
		Player.Character.Torso.CFrame = CFrame.new(math.random(Workspace.Scenery.ReturnPad.Position.X-10,Workspace.Scenery.ReturnPad.Position.X+10),Workspace.Scenery.ReturnPad.Position.Y,math.random(Workspace.Scenery.ReturnPad.Position.Z-10,Workspace.Scenery.ReturnPad.Position.Z+10))
		for i = 0, 1, 0.05 do wait()

3 comments

0

It means that BodyAnchor was not found and that :FindFirstChild returned nil.

0

Also please for the love of god use MaxForce and Position. maxForce and position are deprecated.

0

Its my first few scripts that i am writing out. Where do I find the correct BodyAnchor so I can fix this error than?

1 answer

When you load a game, all the scripts load before the objects, so instead of :FindFirstChild(), use :WaitForChild()

3 comments

0

no

0

but it might be the fix but that isn't always true

0

yea, but in this case it should be the right answer

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