Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to change gravity as the player's Y value goes up?

Hi, I have a large mountain and I'm looking to mess around with the gravity as the user goes up or down this mountain. What i'm not sure of is how to know that the player is going up or down without a while true do loop as it sounds quite inefficient. Is there some listener? As I have terrain, I'm not sure I can make bricks which are invisible. What would you do to achieve this?

3 comments

0

There's no script, so I assume you didn't even try, here at Scripting Helpers we only help people who actually tried to fix their problem. But, eh, I'll give you some tips: The character has a part called "HumanoidRootPart", which has a position, you can use that. And The Workspace has the Gravity property. Hope this helped.

0

Well you can use g * m / d^2 (gravity constant * mass / distance squared). Get the distance from the baseplate on the y axis and change the gravity on the client accordingly.

0

@SuperAndresZ_YT I'm asking about the the theory and the efficient way I can implement my idea- showing no code is completely justified. Here at scripting helpers we are free to discuss starting points for ideas as well as tricks for efficiency. Please reread my question so that you can provide relevant advice on your next attempt; if you have difficulty understanding, then, best is to indicate.

2 answers

I would check every second or so in a loop that iterated through the players and checked their height.

while wait(1) do
	for _,Plr in pairs(game.Players:GetPlayers()) do
		if Plr.Character and Plr.Character:FindFirstChild("HumanoidRootPart") then
			local Height = Plr.Character.HumanoidRootPart.Position.Y
			print(Plr.Name .. " is " .. Height .. " studs above Y = 0")
		end
	end
end

As for adding weightlessness, I would create a BodyFoce inside the HumanoidRootPart. There is an example here on how to make something act entirely weightless, so you could alter that for varying heights.

2 comments

0

I was hoping not to have a while wait or while true loop as that is poor design. Is there an event I can make use of?

0

It is not poor design in situations where you continuously check something, only when you want something to happen after a specific event. Unless you want to use Stepped, which fires every frame and is overkill, while loops are what you should use. 

After checking the wiki I found this.

script.Parent.BodyForce.force = Vector3.new(0, game.Workspace.Gravity, 0) * script.Parent:GetMass()

You could do an if statement,


if game.LocalPlayer.Character.HumanoidRootPart.Position.Y:GetPropertyChangedSignal() then
game.LocalPlayer.Character.BodyForce.force = Vector3.new(0, game.Workspace.Gravity, 0) * game.LocalPlayer.Character.Parent:GetMass()
end

2 comments

0

Thanks, at the moment I'm concerned with how often I should check the Y axis of the player. I was hoping there was an event handler I could take advantage of ? I definitely want to avoid a while true do or while wait do loop.

0

I do suggest a while loop but not a while true or while wait loop, i'm pretty sure you can do a while loop on the property changed signal but not 100% certain

This discussion is preserved as it was. New answers, comments, and votes are closed.