Gravity controller bugs

I am trying to make a working gravity controller. However, my player gets tripped when going near the equator or anywhere lower than 45 degrees of latitude, and the player still got tripped even when I disabled the “Falling”, “GettingUp”, and “Ragdoll” humanoid states.


Another bug in the video is that when I hold the key W, it is not perfectly aligned to the camera.
Also, jumping is very janky in that the walking force overrides the jump, so it is janky.
Finally, the player could “move” into the ground, as their legs sunk into the terrain below.
I tried searching on the forum for this issue, but I could not find anything about it or atleast similar to it.

-- SERVER
local character = script.Parent --CHARACTER
local player = game.Players:GetPlayerFromCharacter(character)
local Humanoid = character:WaitForChild("Humanoid") -- HUMANOID
if Humanoid:IsA("Humanoid") then
	Humanoid:SetStateEnabled(Enum.HumanoidStateType.Ragdoll,false)
	Humanoid:SetStateEnabled(Enum.HumanoidStateType.Flying,false)
	Humanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown,false)
	Humanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp,false)
end
serv = game:GetService("RunService")
--ALIGN ORIENTATION--
local gyro = Instance.new("AlignOrientation",character.Torso) --FOR "PLANET" FEELING
gyro.Mode = Enum.OrientationAlignmentMode.OneAttachment
gyro.Attachment0 = character.HumanoidRootPart.RootAttachment
gyro.MaxTorque = math.huge --SETS MAX TORQUE
gyro.Responsiveness = 30
--LINE FORCE--
local force = Instance.new("LineForce",character.Torso) --INSERTS A FORCE INTO CHARACTER'S TORSO
force.ApplyAtCenterOfMass = true --APPLIES AT CENTER OF MASS (C.O.M)
force.Attachment0 = character.HumanoidRootPart.RootAttachment --SETS ATTACHMENT1 TO CHARACTER'S ROOT ATTACHMENT
force.Attachment1 = nil
dist = nil
mass = 0
distance = nil
rot = 0
val = 0
local movement = Instance.new("BodyVelocity",character.Torso)
movement.P = math.huge
movement.MaxForce = Vector3.new(400000,400000,400000)
function FindNearestBody() --FIND NEAREST CELESTIAL BODY
	local least = nil
	local max = math.huge
	local planets = workspace.Cores:GetChildren()
	local e = 0
	for i,v in pairs(planets) do
		e = (v.Position-character.HumanoidRootPart.Position).Magnitude
		if e < max then
			max = e
			least = v
		end
	end
	return {least,e}
end
function Get(part)
	if part:IsA("BasePart") then
		mass += part.Mass
	end
end
function GetMassOfAssembly(assembly) --GET MASS OF MODEL
	Get(assembly)
	for i,v in pairs(assembly:GetChildren()) do
		Get(assembly)
		GetMassOfAssembly(v)
	end
end
character.Movement.OnServerEvent:Connect(function(plr,re,rotate)
	if plr == player then
		val = rotate
		rot = re
		movement.Velocity = character.HumanoidRootPart.CFrame.LookVector*16
	end
end)
character.StopMovement.OnServerEvent:Connect(function(plr)
	if plr == player then
		movement.Velocity = Vector3.new(0,0,0)
	end
end)
character.Jump.OnServerEvent:Connect(function(plr)
	if not character.HumanoidRootPart:FindFirstChildWhichIsA("BodyVelocity") and plr == player then 
		local jump = Instance.new("BodyVelocity",character.HumanoidRootPart)
		jump.P = math.huge
		movement.MaxForce = Vector3.new(0,0,0)
		jump.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
		jump.Velocity = (character.Head.CFrame.UpVector*mass)/2
		Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		game.Debris:AddItem(jump,.3)
	end
end)
serv.Heartbeat:Connect(function()
	mass = 0
	--GRAVITY AND ROTATION OF GYRO--
	local dist = FindNearestBody()
	local body = dist[1]
	GetMassOfAssembly(character)
	if body ~= nil then
		local att = body:FindFirstChildWhichIsA("Attachment")
		if att and character:FindFirstChild("HumanoidRootPart") then
			if character.Humanoid.FloorMaterial ~= Enum.Material.Air then
				movement.MaxForce = Vector3.new(400000,400000,400000)
			else
				movement.MaxForce = Vector3.new(0,0,0)
			end
			force.Attachment1 = att
			local ae = rot+val
			distance = dist[2]
			print(distance)
			if distance < body.SOIRadius.Value then
				force.Magnitude = ((body.SurfaceGravity.Value*250000000)*mass/(distance*distance)) --SET THE FORCE'S STRENGTH
				gyro.CFrame = CFrame.lookAt(gyro.Attachment0.WorldPosition,att.WorldPosition) * CFrame.Angles(0,math.rad(90),math.rad(90)) * CFrame.Angles(0,math.rad(ae),0)
			else
				force.Magnitude = 0
				
			end
		end
	end
end)
--CLIENT
UIS = game:GetService("UserInputService")
player = game.Players.LocalPlayer
speedFor = 0
speedRight = 0
ori = 0
debounce = false
function GetCamOrientation()
	local cfr = workspace.CurrentCamera.CFrame
	local x,y,z = cfr:ToOrientation()
	ori = math.deg(y)
	return ori
end
UIS.InputBegan:Connect(function(input, GPE) -- This will detect the input, Connecting to the input that got in, GPE = Game Processed Event and is if the game processed it and if the player is busy, like typing in the chat for an example
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if GPE == false then
				if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.S then -- Making a if statement to ask if the players input is == to input
					while UIS:IsKeyDown(Enum.KeyCode.W) do
						GetCamOrientation()
						script.Parent.Movement:FireServer(ori,-90)
						task.wait()
					end
				elseif input.KeyCode == Enum.KeyCode.S then
					while UIS:IsKeyDown(Enum.KeyCode.S) or UIS:IsKeyDown(Enum.KeyCode.W) do
						GetCamOrientation()
						script.Parent.Movement:FireServer(ori,90)
						task.wait()
					end
				elseif input.KeyCode == Enum.KeyCode.A then -- Making a if statement to ask if the players input is == to input
					while UIS:IsKeyDown(Enum.KeyCode.A) do
						GetCamOrientation()
						script.Parent.Movement:FireServer(ori,0)
						task.wait()
					end
				elseif input.KeyCode == Enum.KeyCode.D then
					while UIS:IsKeyDown(Enum.KeyCode.D) do
						GetCamOrientation()
						script.Parent.Movement:FireServer(ori,180)
						task.wait()
					end
				elseif input.KeyCode == Enum.KeyCode.Space then
					while UIS:IsKeyDown(Enum.KeyCode.Space) do
						local human = player.Character:FindFirstChild("Humanoid")
						if debounce == false and human.FloorMaterial ~= Enum.Material.Air then
							debounce = true
							script.Parent.Jump:FireServer()
						end
						task.wait(.5)
						debounce = false
					end
				end
				task.wait()
		end
	end
end)
UIS.InputEnded:Connect(function(input, GPE) -- This will detect the input, Connecting to the input that got in, GPE = Game Processed Event and is if the game processed it and if the player is busy, like typing in the chat for an example
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if GPE == false then
			script.Parent.StopMovement:FireServer()
		end
	end
end)

I think the natural tendency of the Humanoid to stay upright is fighting with you. Try also putting in an AlignPosition. I’ve noticed similar things even when setting the gravity to 0 or a tiny number.

Yeah, for some reason, even when I disabled the “GettingUp” state, it was still stuck in it.

However, I am wondering, how would I go about using an AlignPosition? Would I make it move to the gravity’s center or above the player?

Okay, so I tried fixing the jumping only going up, and now I get an error. Here is my code:

--SERVER
local character = script.Parent --CHARACTER
local player = game.Players:GetPlayerFromCharacter(character)
local Humanoid = character:WaitForChild("Humanoid") -- HUMANOID
if Humanoid:IsA("Humanoid") then
	Humanoid:SetStateEnabled(Enum.HumanoidStateType.Ragdoll,false)
	Humanoid:SetStateEnabled(Enum.HumanoidStateType.FallingDown,false)
	Humanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp,false)
end
serv = game:GetService("RunService")
--ALIGN ORIENTATION--
local gyro = Instance.new("AlignOrientation",character.Torso) --FOR "PLANET" FEELING
gyro.Mode = Enum.OrientationAlignmentMode.OneAttachment
gyro.Attachment0 = character.HumanoidRootPart.RootAttachment
gyro.MaxTorque = math.huge --SETS MAX TORQUE
gyro.Responsiveness = 30
local position = Instance.new("AlignPosition",character.Torso) --FOR "PLANET" FEELING
position.Mode = Enum.PositionAlignmentMode.OneAttachment
position.Attachment0 = character.HumanoidRootPart.RootAttachment
--position.MaxForce = 4000
position.MaxForce = 0
--LINE FORCE--
local force = Instance.new("LineForce",character.Torso) --INSERTS A FORCE INTO CHARACTER'S TORSO
force.ApplyAtCenterOfMass = true --APPLIES AT CENTER OF MASS (C.O.M)
force.Attachment0 = character.HumanoidRootPart.RootAttachment --SETS ATTACHMENT1 TO CHARACTER'S ROOT ATTACHMENT
force.Attachment1 = nil
dist = nil
mass = 0
distance = nil
rot = 0
val = 0
local movement = Instance.new("BodyVelocity",character.Torso)
movement.P = math.huge
movement.MaxForce = Vector3.new(400000,400000,400000)
function FindNearestBody() --FIND NEAREST CELESTIAL BODY
	local least = nil
	local max = math.huge
	local planets = workspace.Cores:GetChildren()
	local e = 0
	for i,v in pairs(planets) do
		e = (v.Position-character.HumanoidRootPart.Position).Magnitude
		if e < max then
			max = e
			least = v
		end
	end
	return {least,e}
end
function Get(part)
	if part:IsA("BasePart") then
		mass += part.Mass
	end
end
function GetMassOfAssembly(assembly) --GET MASS OF MODEL
	Get(assembly)
	for i,v in pairs(assembly:GetChildren()) do
		Get(assembly)
		GetMassOfAssembly(v)
	end
end
character.Movement.OnServerEvent:Connect(function(plr,re,rotate)
	if plr == player then
		val = rotate
		rot = re
		movement.Velocity = character.HumanoidRootPart.CFrame.LookVector*16
	end
end)
character.StopMovement.OnServerEvent:Connect(function(plr)
	if plr == player then
		movement.Velocity = Vector3.new(0,0,0)
	end
end)
character.Jump.OnServerEvent:Connect(function(plr)
	if plr == player then 
		local jump = movement
		jump.P = math.huge
		movement.MaxForce = Vector3.new(0,0,0)
		Humanoid:ChangeState(Enum.HumanoidStateType.Jumping)
		for i = 1,200 do
			jump.MaxForce = Vector3.new(math.huge,math.huge,math.huge)
			print((character.Head.CFrame*CFrame.new(0,4,0)*mass)/2)
			jump.Velocity = ((character.Head.CFrame*CFrame.new(0,2*mass,0)))
		end
	end
end)
serv.Heartbeat:Connect(function()
	mass = 0
	--GRAVITY AND ROTATION OF GYRO--
	local dist = FindNearestBody()
	local body = dist[1]
	GetMassOfAssembly(character)
	if body ~= nil then
		local att = body:FindFirstChildWhichIsA("Attachment")
		if att and character:FindFirstChild("HumanoidRootPart") then
			if character.Humanoid.FloorMaterial ~= Enum.Material.Air then
				movement.MaxForce = Vector3.new(400000,400000,400000)
			else
				movement.MaxForce = Vector3.new(0,0,0)
			end
			force.Attachment1 = att
			local ae = rot+val
			distance = dist[2]
			--position.Position = character.Head.Position+character.Head.CFrame.*12
			if distance < body.SOIRadius.Value then
				force.Magnitude = ((body.SurfaceGravity.Value*250000000)*mass/(distance*distance)) --SET THE FORCE'S STRENGTH
				gyro.CFrame = CFrame.lookAt(gyro.Attachment0.WorldPosition,att.WorldPosition) * CFrame.Angles(0,math.rad(90),math.rad(90)) * CFrame.Angles(0,math.rad(ae),0)
			else
				force.Magnitude = 0
				gyro.CFrame = CFrame.Angles(0,math.rad(ae),0)
			end
		end
	end
end)
--CLIENT
UIS = game:GetService("UserInputService")
player = game.Players.LocalPlayer
speedFor = 0
speedRight = 0
ori = 0
debounce = false
function GetCamOrientation()
	local cfr = workspace.CurrentCamera.CFrame
	local x,y,z = cfr:ToOrientation()
	ori = math.deg(y)
	return ori
end
UIS.InputBegan:Connect(function(input, GPE) -- This will detect the input, Connecting to the input that got in, GPE = Game Processed Event and is if the game processed it and if the player is busy, like typing in the chat for an example
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if GPE == false then
				if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.S then -- Making a if statement to ask if the players input is == to input
					while UIS:IsKeyDown(Enum.KeyCode.W) do
						GetCamOrientation()
						script.Parent.Movement:FireServer(ori,-90)
						task.wait()
					end
				elseif input.KeyCode == Enum.KeyCode.S then
					while UIS:IsKeyDown(Enum.KeyCode.S) or UIS:IsKeyDown(Enum.KeyCode.W) do
						GetCamOrientation()
						script.Parent.Movement:FireServer(ori,90)
						task.wait()
					end
				elseif input.KeyCode == Enum.KeyCode.A then -- Making a if statement to ask if the players input is == to input
					while UIS:IsKeyDown(Enum.KeyCode.A) do
						GetCamOrientation()
						script.Parent.Movement:FireServer(ori,0)
						task.wait()
					end
				elseif input.KeyCode == Enum.KeyCode.D then
					while UIS:IsKeyDown(Enum.KeyCode.D) do
						GetCamOrientation()
						script.Parent.Movement:FireServer(ori,180)
						task.wait()
					end
				elseif input.KeyCode == Enum.KeyCode.Space then
					while UIS:IsKeyDown(Enum.KeyCode.Space) do
						local human = player.Character:FindFirstChild("Humanoid")
						if debounce == false and human.FloorMaterial ~= Enum.Material.Air then
							debounce = true
							script.Parent.Jump:FireServer()
						end
						task.wait(.5)
						debounce = false
					end
				end
				task.wait()
		end
	end
end)
UIS.InputEnded:Connect(function(input, GPE) -- This will detect the input, Connecting to the input that got in, GPE = Game Processed Event and is if the game processed it and if the player is busy, like typing in the chat for an example
	if input.UserInputType == Enum.UserInputType.Keyboard then
		if GPE == false then
			script.Parent.StopMovement:FireServer()
		end
	end
end)

Why not use EgoMoose’s gravity controller

I do not want people walking on the side of their spaceship too since it sticks on walls and any parts, and I want the gravity to get weaker as you go farther away from the planet’s or the star’s center, similar to real life.

Someone suggested I disable EvaluateStateMachine for the tripping below 45 degrees of latitude and disabling PlatformStand so that the player’s legs don’t sink, and it seems to work. However, I am still faced with the jumping problem and now the LineForce has a magnitude of 0.

Small change: It turned out the LineForce magnitude problem had to do with the mass function not working properly.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.