Help with a simple script

I wanted to make a script that does this: When you touch a part the gravity value changes (workspace.Gravity), but I want to make it to be loca, I mean, only the player that touched the part can experience other gravity value.

When I made it, didnt work as a “local” script, all the players experienced a change of gravity.

I tried something like making a script that says when you touch the part, other local script changes to “disabled = false”, and that local script changes de gravity. It didnt work, the local script changed to “disabled = false” (it started running) but the gravity didnt change.

I also tried to not doing it with a local script, and as I said before, all the players experienced a change of gravity.

(I’m a begginer, so I’m just starting to script.)

Can I have any help to fix that? Thanks.

1 Like

from what I know, instead of making so that as soon as a part was touched and it changes the gravity for everyone, you need to use the “FindFirstChild” function, from which it will affect the player that touched it, instead of others. I don’t know how to write those kinds of scripts just yet, but finding tutorials about these will be very useful and help you understand the function better

1 Like

This can be achieved by using a RemoteEvent in order to change the gravity for that one player. RemoteEvents can either be fired from the Client to the Server, or from the Server to the Client. In this case, we will be firing the RemoteEvent from the Server to the Client.

Firstly, you need to insert a RemoteEvent into ReplicatedStorage for these examples. For it to work with the examples below, this RemoteEvent must be called “GravityEvent”, however you can change the name and edit the code if you wish.

-- This is a normal script. For this example, it is placed inside the part that is going to be touched.

local part = script.Parent

part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then -- Checking if there is a Humanoid in the character
        local player = game.Players:GetPlayerFromCharacter(hit.Parent)
        game.ReplicatedStorage.GravityEvent:FireClient(plr)
    end
end)

Using the script above, you are firing a RemoteEvent from the Server to the Client. Now, you need to recognise when it is fired and do something on the client.

-- This is a LocalScript. It is placed inside StarterGui.

game.ReplicatedStorage.GravityEvent.OnClientEvent:Connect(function()
    game.Workspace.Gravity = 0 -- Change the number
end)

This will result in a gravity change for the player who touched the brick, but on the Server the gravity will remain the same. This means only the person who touched the brick will experience the gravity change.

This is a basic use case for RemoteEvents. If you want to learn more about RemoteEvents, I recommend you take a look at the API Reference article on them, as well as tutorials on YouTube. You can also learn about RemoteEvents and RemoteFunctions here.

Hopefully this was helpful.