Changing gravity for only one player

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.

Just curious; is this a teleport or does gravity turn off when you leave a certain area

I don’t understand your question. It changes the gravity for one player when they touch a part.

A simple way is to make an invisible block which the user touches and it changes their gravity.

debounce = true

function onTouched(hit)
local h = hit.Parent:findFirstChild(“Humanoid”)
if (h ~= nil and debounce == true) then
debounce = false
h.JumpPower = 50 //Just change this value//
wait(1)
debounce = true
end
end

script.Parent.Touched:connect(onTouched)

1 Like

That doesn’t change the gravity, it changes the JumpPower. Also we have already found a solution to the problem.

gravity blocks.rbxm (2.9 KB)

2 Likes

Oh alright, I will keep that in mind.