Need help changing Gravity

Hey there everyone. For the past few weeks I’ve been trying to create a gravity game, however I’ve run into a problem recently with part of my code and can’t figure it out. I’ve tried for the past 4-5 days and have looked all over the place but can’t find anything. Here’s the code:

local GravityResetParts = workspace.GravityResetParts


for i, v in pairs(GravityResetParts:GetChildren()) do
	print(v.Name)
	v.Touched:Connect(function(hit)
		if hit.Parent:FindFirstChild("Humanoid") and hit.Parent == character then
			if hit.Parent.ResetG.Value == true then
				character.Humanoid.JumpPower = 50
				workspace.Gravity = 196.2
				hit.Parent.ResetG.Value = false
				wait(2)
				v.Parent = nil
				hit.Parent.ResetG.Value = true
				print("Reset g value")
			end
		end
	end)
end

The variable GravityResetParts is referring to a model in the workspace with all the parts that are supposed to reset the gravity when a player touches it. This is all in a local script. I’m pretty sure changes to physics automatically replicate to the server. The problem here is that the ResetGValue, located in the character, doesn’t always return back to true after 2 seconds. It does sometimes, but not all the time. This causes the player to continually float, and since I have death parts in the game, they end up floating straight into the death parts. I’m wondering if it has something to do with the for loop, but I’m not sure what other way I could go through a model.

If anyone needs anymore clarification on what it does, or more code, then please tell me because this has been a problem for such a long time and if I can’t get it fixed, it’ll ruin my game. Thanks.

I think this might have something to do with your character variable. If you are creating the variable at the start of the script, it will never update when a new character is created. Try using a direct game.Players.LocalPlayer check instead of the variable.

1 Like

Thanks for the reply. I forgot to mention this local script was in StarterCharacterScripts, so wouldn’t it automatically get regenerated every time the character gets regenerated, or is my logic flawed there?

I think I solved the problem actually. After posting, I realized since I was setting the Parent property of V to nil, I didn’t technically need the ResetGValue anymore since the part was basically being destroyed after it was touched, so it could only reset the gravity once. I also moved the part of the code that changes the gravity back to 0 from Server side to Client Side, and I’m now handling everything on the client. So far, I have a 100% success rate in properly resetting the gravity to 196.2 (the default value), and then back to 0 (no gravity), whereas usually the success rate would be around 40-60%.

Nevertheless, thank you for the reply, and I’ve added your suggestion into the code.

1 Like