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)
This is what I tried so far