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.
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.
- 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.
- 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)