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

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

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

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)

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