Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 5 years ago

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()
0
It means that BodyAnchor was not found and that :FindFirstChild returned nil. User#21908 42 — 5y
0
Also please for the love of god use MaxForce and Position. maxForce and position are deprecated. User#19524 175 — 5y
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? DevBuildr 0 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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

0
no User#19524 175 — 5y
0
but it might be the fix but that isn't always true User#19524 175 — 5y
0
yea, but in this case it should be the right answer supercoolboy8804 114 — 5y
Ad

Answer this question