You can write your topic however you want, but you need to answer these questions:
What do you want to achieve?
: I want to be able to switch gravity at press of a button. When I press a button I want a force to pull me upwards.
What is the issue?
: I can’t figure out how to do that.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
: So far I’ve tried to look in scripts that are similar but I still can’t understand and I’ve searched on the dev forum already and I’ve also tried using VectorForce but it didn’t get to far.
local UserInputService = game:GetService("UserInputService")
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local humanoid = character:WaitForChild("Humanoid")
local gravityFlipped = false
local vectorForce = Instance.new("VectorForce") -- Use BodyForce to apply continuous force
vectorForce.Force = Vector3.new(0, 0, 0)
vectorForce.Parent = humanoidRootPart
task.wait(1)
local attPart = Instance.new("Part", character)
attPart.Anchored = true
attPart.size = Vector3.new(0.2,.2,.2)
local att = Instance.new("Attachment", attPart)
vectorForce.RelativeTo = Enum.ActuatorRelativeTo.Attachment0
vectorForce.Attachment0 = att
att.Orientation = Vector3.new(0,0,90)
att.Axis = Vector3.new(-0,1,0)
att.SecondaryAxis = Vector3.new(-1,-0,0)
-- Function to flip gravity
local function flipGravity()
gravityFlipped = not gravityFlipped
if gravityFlipped then
-- Apply upward force equal to the character's weight
vectorForce.Force = Vector3.new(0, humanoidRootPart.AssemblyMass * workspace.Gravity, 0)
humanoidRootPart.CFrame = humanoidRootPart.CFrame * CFrame.Angles(math.pi, 0, 0) -- Flip orientation
else
-- Remove the upward force
vectorForce.Force = Vector3.new(0, 0, 0)
humanoidRootPart.CFrame = humanoidRootPart.CFrame * CFrame.Angles(-math.pi, 0, 0) -- Reset orientation
end
end
-- Detect when the player presses E
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if gameProcessed then return end -- Ignore inputs that are already processed
if input.KeyCode == Enum.KeyCode.E then
flipGravity()
end
end)
What happens when using the code you already have? Does the character get rotated correctly?
Looking at the code, you should be multiplying the force by 2. Currently, it only has enough force to keep you from falling due to gravity, however it doesn’t have the force required to move you in the opposite direction of normal gravity.
That’s because the attachment has to be a child of HumanoidRootPart the force will apply to the attachment’s parent not the Constraint’s parent.
Just set the Attachment0 to HumanoidRootPart.RootAttachment
you can do this by using a linearvelocity set with ForceLimitMode.PerAxis using just the Y axis for force and vectorvelocity
and if you use an attribute it can be controlled easily by setting an attribute or setting it back to false or nil
here is an example you can use this script in a localscript and put it in the StarterPlayerScripts
should also still work if character is reset
local player = game.Players.LocalPlayer
function SetupCharacter(character)
local HRP = character:WaitForChild('HumanoidRootPart')
local RA = HRP:WaitForChild('RootAttachment')
local linearVelocity = character:FindFirstChild('AGVelocity') or Instance.new('LinearVelocity')
linearVelocity.Name = 'AGVelocity'
linearVelocity.Attachment0 = RA
linearVelocity.ForceLimitMode = Enum.ForceLimitMode.PerAxis
linearVelocity.MaxAxesForce = Vector3.new(0,100000,0) -- force applied to y axis
linearVelocity.VectorVelocity = Vector3.new(0,10,0) -- velocity to move player by on Y axis -- if you negative this it can make a controled decent...
linearVelocity.Enabled = false -- keep it off until attrubute is set
linearVelocity.Parent = HRP
end
player:GetAttributeChangedSignal('AntiGravity'):Connect(function() -- enable or disable antigravity
local linearVelocity = player.Character:FindFirstChild('AGVelocity', true) -- look under character for the agvelocity
if not linearVelocity then
return
end
if player:GetAttribute('AntiGravity') then
linearVelocity.Enabled = true
else
linearVelocity.Enabled = false
end
end)
SetupCharacter(player.Character or player.CharacterAdded:Wait()) -- initial call to setup first character
player.CharacterAdded:Connect(function(character) -- this is for reconnection to character incase they die
SetupCharacter(character)
end)
-- you can remove this and call it from any script just set or unset this attribute on the player you want to have anti gravity or not
task.wait(5)
player:SetAttribute('AntiGravity', true) -- this is what turns it on and off you can set this from any script local or server
task.wait(5)
player:SetAttribute('AntiGravity', nil) -- this turns it off can also set this to false
Yeah, Thank you for helping me on my last 2 posts one of the problems was when I rotate the HumanoidRootPart the linearVelocity will be pulling the player down.