What’s the easiest way to change a players local gravity if they have a specific gamepass that’s supposed to lower their gravity? I tried using JumpPower but I just realized that won’t change how fast they fall, but I want their gravity to be set to 50.
Edit: I figured out all you have to do is fire a remote event from the server to the client, then the game.Workspace.Gravity value can be set to anything for a local player.
This should work in a LocalScript
Since this is running on the client the gravity should change localy
local gamepassId = 1083944
local mps = game:GetService(“MarketplaceService”)
game.Players.PlayerAdded:Connect(function(plr)
if mps:UserOwnsGamePassAsync(plr.UserId,gamepassId) then
plr.CharacterAdded:Connect(function(char)
game.Workspace.Gravity = 70 --The gravity
wait(1)
end)
end
end)
The workspace has property called Gravity, you could try setting that via a local script. But I’d recommend doing it the old way with a body force that you add into the player’s root part that only slightly pushes them up and will lower the effect gravity has on them.
Instead of changing the player’s JumpPower, you could insert a BodyForce into their torso (or the HumanoidRootPart) to mitigate some of the gravity. This would make the player behave as if they constantly had a Gravity Coil equipped
If you change the game’s gravity from the server, it will apply to all players which is not what you want. So you might want to use a remote, and if a player meets a certain requirement (in this case owning the gamepass) you fire an event which will change the player’s gravity.
The server script:
local MarketPlaceService = game:GetService("MarketplaceService")
local gamepassId = 1083944
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function()
wait(1) -- You might want to experiment with this. Personally the scripts are not for sure loaded when the player respawns on the server, so I just add this as a precaution
if MarketPlaceService:UserOwnsGamePassAsync(player.UserId, gamepassId) then
game.ReplicatedStorage.ChangeGravity:FireClient(player, 50)
end
end)
end)
The local script in StarterPlayer>StarterCharacterScripts
local gravityRemote = game.ReplicatedStorage:WaitForChild("ChangeGravity")
gravityRemote.OnClientEvent:Connect(function(gravity)
game.Workspace.Gravity = gravity
end)
Obviously, exploiters can easily change the gravity how they want with no repercussions at ease. I recommend adding a few anti-cheats, but there really is no for sure way to combat it.
If you’d want a script to set a client’s gravity, you’d want the server script to fire a remote event. Then, when the client gets the signal for the remote event, it sets the gravity locally.
Take this for example:
-- server
local gamepassId = 1083944
local marketplaceService = game:GetService("MarketplaceService")
local playerService = game:GetService("Players")
local gravityEvent= Instance.new("RemoteEvent", game:GetService("ReplicatedStorage"))
gravityEvent.Name = "SetGravity"
function setGravity(player, amount)
gravityEvent:FireClient(player, amount)
end
playerService.PlayerAdded:Connect(function(player)
if marketplaceService:UserOwnsGamePassAsync(player.UsreId, gamepassId) then
local gravity = 50
setGravity(player, gravity)
end
end)
-- client
local workspace = game:GetService("Workspace")
local gravityEvent = game:GetService("ReplicatedStorage"):WaitForChild("SetGravity")
gravityEvent.OnClientEvent:Connect(function(amount)
workspcae.Gravity = amount
end)
Edit: I have just seen @DevKaister 's post, and it seems to do a good job at explaining what I’m trying to convey to you.
From experience with using PlayerAdded from a local script and failing using the same method, I believe that the event would not fire until another player is joining the server because the player has already been added once the script is running. You can just go straight to checking the LocalPlayer since this is a local script.
This script would wait for another player besides that of the client to be added to the game and then check them. If they own the game pass, then the local player would receive have a set gravity of 70 without owning the game pass.
Therefore, there’s no use in attempting to fire the PlayerAdded event. Just this part would be fine!
local gamepassId = 9024798
local mps = game:GetService("MarketplaceService")
plr = game.Players.LocalPlayer
if mps:UserOwnsGamePassAsync(plr.UserId,gamepassId) then
game.Workspace.Gravity = 0
end
Using remotes would also work just as well as previously mentioned, although both of these would have the exact same function. (But remotes would be more useful if you’re wishing to fire the event at a specific point in time from a serverscript)
Lua Learning has a tutorial on how to determine and negate gravity’s effect on individual objects, found here:
It teaches how to use forces to calculate the weight and completely negate it with a force in the opposite direction. In your case, you would just weaken your force a little so gravity wins eventually but acts much lower.
Having the force act on the HumanoidRootPart allows the player to move like low gravity, without making every other object in their world act floaty too.