Changing gravity for only one player

I think what he means is that your solution is flawed.

Not in the sense that it won’t work but in the sense that, not to be offensive to Rainzyyy (not my intention at all) or anything, but if he hasn’t already explored some of the basic solutions we have listed here (like setting gravity in a LS), how do you expect him to be able to follow suit with your solution?

As @emojipasta said too, introducing BodyForces, BodyMovers, things like that make managing “gravity” difficult and some aspects of anti-fly exploits difficult to include. Setting workspace.Gravity is easier and offers pretty similar levels of security (and as he mentioned, possibly even more).

2 Likes

Here is an example, have this as a local script inside the part you want to be touched:

local part = script.Parent

part.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild("Humanoid") then
workspace.Gravity = 50 -- ur gravity here
end
end)

Hope this can help, I might be wrong.

1 Like

What are you even trying to conclude with this statement?

Local scripts don’t execute in a part in workspace unless it’s a descendant of the character. The LocalScript has to be in a place like StarterCharacterScripts or the script has to be a server script with a remove event attached.

I don’t know about the consequences of repeatedly setting the gravity, but a script like that should have a simple check to ensure it only runs once (if gravity ~= 50 then gravity = 50 end)

I said your argument of “you can use them to apply any amount of force in any direction. You can’t use workspace.Gravity to reverse the force anyhow” (which equates to saying only BodyForces will allow OP to define a negative gravity) is nonsensical, since negative gravity (=> gravity/“a force” pointing upward) doesn’t exist.

workspace.Gravity = 50

local Plr = workspace:WaitForChild(game.Players.LocalPlayer.Name)
local Mass = 0

local BodyForce = Instance.new("BodyForce")
BodyForce.Parent = Plr:WaitForChild("HumanoidRootPart")

local AllParts = Plr:GetChildren()
for index = 1, #AllParts do
	if AllParts[index]:IsA("BasePart") then
		Mass += AllParts[index]:GetMass()
	end
end

BodyForce.Force = Vector3.new(0, (workspace.Gravity * Mass)/2, 0)

While the end result may look almost the same, the performance cost using the depreciated body movers is much higher than simply changing the gravity.

Changing the gravity:

Bodymover:
image

Really you should be using the new constraint movers however they move relative to the player so if they tilt they will likely be dragged slightly in that direction.

2 Likes

That code won’t do.

  • LocalScripts don’t run under non-client areas (like the workspace).
  • Even if it didn’t work, it would trigger the gravity-setting code even if the LocalPlayer wasn’t the one who triggered it, since it only looks for any generic Humanoid and not the LocalPlayer’s own one.

Ah yes I forgot we are trying to access the workspace, I’m slacking here.

local part = workspace:WaitForChild("Part")

part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if workspace.Gravity ~= 10 then 
			workspace.Gravityy = 10 -- ur gravity here
		end
	end
end)

Better solution here, yet I’m unsure about having a bad side of changing the gravity everytime its touched, but I guess that’s better since we have multiple parts touching.

@Rainzyyy Try having this as a local script inside PlayerStarterScripts, should work perfectly.

4 Likes

local Gravity = workspace.Gravity assigns the value of the gravity to the variable, not the reference. Your script is just changing your own variable, doing nothing. If you replace the Gravity with workspace.Gravity throughout it will work perfectly for OP.

2 Likes

Hmm, never knew about that one, thanks for the note tho.

That new code won’t work either.

local Gravity = workspace.Gravity

This returns a value, not a property. Changing it by doing Gravity = 10 won’t change the Gravity property of the workspace but rather the value of the variable. Additionally, as mentioned earlier, it would trigger the gravity-modifier on all clients since it doesn’t check that the humanoid in question belongs to the LocalPlayer.

Edit:
note: emojipasta already pointed out the first issue

1 Like

Could you point out where I said “Negative gravity” in any of my messages? By reversing a force I mean many other applications other than gravity. You can have further control over your character just not for gravity using the same force. This argument is pointless so Ill move out.

This sure has a lot of replies for
game.Workspace.Gravity = 0.5

1 Like

He was trying to make a part get touched change the gravity for a single player which he didn’t clearify at first, but welp :man_shrugging:.

also workspace.Gravity = 0.5 :3

This changes the gravity for everyone, I want it to only change for the person who touched the part.

local part = workspace:WaitForChild("Part")

part.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if workspace.Gravity ~= 10 then 
			workspace.Gravityy = 10 -- ur gravity here
		end
	end
end)

This script^^

Put this in a LocalScript in StarterCharacterScripts:

local part = workspace:WaitForChild("Part")

part.Touched:Connect(function(hit)
	if hit:IsDescendantOf(script.Parent) then
		if workspace.Gravity ~= 10 then 
			workspace.Gravity = 10
		end
	end
end)

Working LocalScript:

local part = workspace:WaitForChild("Part")

part.Touched:Connect(function(hit)
	if (hit.Parent:FindFirstChildWhichIsA("Humanoid"))  and (hit.Parent:FindFirstChild("Humanoid") == game.Players.LocalPlayer.Character:FindFirstChild("Humanoid"))  then
		if workspace.Gravity ~= 100 then 
			workspace.Gravity = 100-- your gravity here
		end
	end
end)
8 Likes

I’m pretty sure I told you to have this as a local script inside StarterPlayerScripts
:thinking:

It still affects everyone because it doesn’t check that the humanoid belongs to the LocalPlayer, just that it is a humanoid to begin with.

I did put it in StarterPlayerScripts as a local script.