How do I fix this issue?

I’m trying to make it so that when a player turns into a ghost, they get a script that makes it so that their (gravity * total mass) is decreased by 1/5th, but I’m having an issue with making that happen.

  1. The humanoid jumping property
  • I tried to make it so that whenever it was fired, the linear velocity would be enabled with the canceled out gravitational force and then it would be destroyed after touching one of the player’s legs, but for some reason since the Humanoid:StateChanged() event happens when the state changes, if I’m running then the state will be jumping when it wants to.
  1. The force… doubles??
  • For some reason the attachments and the linear velocity force some times doubles instead of just making one of each and then later destroying itself, I have no idea why this happens.
  1. For some reason it takes a bit for the script to load (I’m assuming), which makes the issue of the script not initially working when I jump right after I get the script cloned into my character model.

Here’s the script

local character = script.Parent
local humanoid = character:FindFirstChildOfClass("Humanoid")
local jumping = false

-- makes it so that the script destroys itself after 10 seconds (thats how long the ghost form lasts)
task.spawn(function()
	task.wait(10)

	script:Destroy()
end)

local TotalMass = 0 -- the total mass of the character

for _, Part in pairs(character:GetDescendants()) do
	if Part:IsA("BasePart") then
		TotalMass += Part:GetMass()
	end
end

local newJumpForce = TotalMass * workspace.Gravity * .20 -- the formula for giving them a 1/5 decrease in gravity.

humanoid.Jumping:Connect(function()
	if not jumping then
		print("jumping")
		jumping = true
		
		local linearVelocity = Instance.new("LinearVelocity") -- The force that decreases gravity * total mass by 1/5
		linearVelocity.Parent = character.PrimaryPart
		linearVelocity.Name = "GhostJumpForce"

		linearVelocity.VectorVelocity = Vector3.new(0, newJumpForce, 0)

		linearVelocity.Enabled = true

		local att1 = Instance.new("Attachment", character.PrimaryPart)
		local att2 = Instance.new("Attachment", character.PrimaryPart)

		linearVelocity.Attachment0 = att1
		linearVelocity.Attachment1 = att2
		
		-- The two parts that delete the attatchments
		character["Right Leg"].Touched:Connect(function()
			if linearVelocity and att1 then
				att1:Destroy()
				att2:Destroy()
				linearVelocity:Destroy()
			end
		end)

		character["Left Leg"].Touched:Connect(function()
			if linearVelocity and att1 then
				att1:Destroy()
				att2:Destroy()
				linearVelocity:Destroy()
			end
		end)
		
		jumping = false
	end
end)
3 Likes

Fixed the jumping issue by just reversing the process and using 4/5 instead of 1/5

4 Likes

Why do you need to change it only when they jump? Instead, you could just have the player constantly have the force going upwards. The low gravity sorta effect will only be noticable when the player is in the air anyway.

3 Likes
local character = script.Parent
local humanoid = character:FindFirstChildOfClass("Humanoid")
local jumping = false

task.spawn(function()
	task.wait(10)
	script:Destroy()
end)

local TotalMass = 0

for _, Part in pairs(character:GetDescendants()) do
	if Part:IsA("BasePart") then
		TotalMass += Part:GetMass()
	end
end

local newJumpForce = TotalMass * workspace.Gravity * .20

local function cleanUpForce(linearVelocity, att1, att2)
	if linearVelocity then linearVelocity:Destroy() end
	if att1 then att1:Destroy() end
	if att2 then att2:Destroy() end
	jumping = false
end

humanoid.Jumping:Connect(function()
	if not jumping then
		print("jumping")
		jumping = true
		
		local linearVelocity = Instance.new("LinearVelocity", character.PrimaryPart)
		linearVelocity.Name = "GhostJumpForce"
		linearVelocity.VectorVelocity = Vector3.new(0, newJumpForce, 0)
		linearVelocity.Enabled = true

		local att1 = Instance.new("Attachment", character.PrimaryPart)
		local att2 = Instance.new("Attachment", character.PrimaryPart)

		linearVelocity.Attachment0 = att1
		linearVelocity.Attachment1 = att2

		local function onTouch()
			cleanUpForce(linearVelocity, att1, att2)
		end
		
		character["Right Leg"].Touched:Connect(onTouch)
		character["Left Leg"].Touched:Connect(onTouch)
	end
end)

1 Like

Wait a second, why don’t you just decrease the Workspace.Gravity from a local script?

1 Like

Having a force just seems much easier and also I didn’t know that decreasing it in a local script would decrease the gravity for that player until now.