General > Developers Corner

Changing the global gravity (for players) with Entity:SetGravity() ?

(1/1)

FlyinC4T:
alright so I have been trying out multiple times to get this to work.


--- Code: -----[[
    Gravity Settings--]]
hook.Add("PlayerSpawn", "wzm:GlobalGravity", function()
    Player:SetGravity(100)
end)
--- End code ---

basically it's supposed to set the global gravity for all players. I would use PlayerConnect aswell, but before that I have to make the PlayerSpawn work first.

(I have a bunch of E2 experience and I am aware that it is NOT the same, that being said I should be able to understand at least some stuff)

The Error:

--- Code: ---lua:4: attempt to index global 'Player' (a function value)
--- End code ---


IF IT IS NOT POSSIBLE (read me)
then please do show me alternative ways

DerMetelGamerYT:
Hi, you should try this:


--- Code: -----[[Gravity Settings--]]
hook.Add("PlayerSpawn", "wzm:GlobalGravity", function(player)
    player:SetGravity(100)
end)

--- End code ---

Codingale:

--- Quote from: DerMetelGamerYT on February 16, 2022, 04:10:31 AM ---Hi, you should try this:


--- Code: -----[[Gravity Settings--]]
hook.Add("PlayerSpawn", "wzm:GlobalGravity", function(player)
    player:SetGravity(100)
end)

--- End code ---

--- End quote ---

I advise newcomers to Lua to avoid using globals such as player as if they overwrite the player table they can no longer use things like player.GetAll() and so on, and sometimes they won't even know what's wrong. In most cases I use pl, or ply

iViscosity:
In addition to using "pl" or "ply" (the latter usually being the most common), one note I will mention as to why the original doesn't work is because "Player" in the global namescape refers to Global.Player which is a function (hence the error mentioning "a function value"). The wiki page for GM:PlayerSpawn takes a "Player player" parameter, which can be confusing for those new to Lua. The capitalized "Player" in this case is referring to something that belongs to the Player "class" (quote because Lua has class-like structures but does not technically have classes). However, the engine automatically passes an object that is a part of that class for use within the hook (but the name, like all variables, can be just about anything).

Navigation

[0] Message Index

Go to full version