Ulysses

General => Developers Corner => Topic started by: FlyinC4T on February 15, 2022, 09:11:35 AM

Title: Changing the global gravity (for players) with Entity:SetGravity() ?
Post by: FlyinC4T on February 15, 2022, 09:11:35 AM
alright so I have been trying out multiple times to get this to work.

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

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: [Select]
lua:4: attempt to index global 'Player' (a function value)

IF IT IS NOT POSSIBLE (read me)
then please do show me alternative ways
Title: Re: Changing the global gravity (for players) with Entity:SetGravity() ?
Post by: DerMetelGamerYT on February 16, 2022, 04:10:31 AM
Hi, you should try this:

Code: [Select]
--[[Gravity Settings--]]
hook.Add("PlayerSpawn", "wzm:GlobalGravity", function(player)
    player:SetGravity(100)
end)
Title: Re: Changing the global gravity (for players) with Entity:SetGravity() ?
Post by: Codingale on April 22, 2022, 08:29:03 PM
Hi, you should try this:

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

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
Title: Re: Changing the global gravity (for players) with Entity:SetGravity() ?
Post by: iViscosity on July 12, 2022, 11:57:35 AM
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 (https://wiki.facepunch.com/gmod/Global.Player) which is a function (hence the error mentioning "a function value"). The wiki page for GM:PlayerSpawn (https://wiki.facepunch.com/gmod/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).