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

Gravity script that enables when reaching a certain height doesn't work?

Asked by 6 years ago

Hello!

I try to make a gravity script that enables gravity when you reach a certain height. I have a part in the air that enables this function when a player touches this. However this doesn't work. I keep recieving the error: 19:21:48.959 - Workspace.Script:2: attempt to index a nil value

My script:

game.Workspace.GravityLine.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("UpperTorso"):FindFirstChild("BodyForce") then
        hit.Parent:FindFirstChild("UpperTorso"):FindFirstChild("BodyForce").Force = Vector3.new(0,2000,0)
    else
        local bodyForce = Instance.new("BodyForce",hit.Parent:FindFirstChild("Uppertorso"))
        bodyForce.Force = Vector3.new(0,2000,0)
    end
end)

Thanks for helping!

Kees

1 answer

Log in to vote
0
Answered by 6 years ago

Found a solution thanks to @gulllet

 game.Workspace.GravityLine.Touched:Connect(function(hit)

    local UT = hit.Parent:FindFirstChild("UpperTorso")

    if UT and UT:FindFirstChild("BodyForce") then
        UT:FindFirstChild("BodyForce").Force = Vector3.new(0,2000,0)
        print("Bodyforce updated")

    elseif hit.Parent:FindFirstChild("Humanoid")then
        local bodyForce = Instance.new("BodyForce")
        bodyForce.Parent = hit.Parent:FindFirstChild("UpperTorso")
        bodyForce.Force = Vector3.new(0,2000,0)
        print("BodyForce Added")
        print("GravityChanged")
        end
     end)
Ad

Answer this question