Author Topic: Changing the global gravity (for players) with Entity:SetGravity() ?  (Read 3490 times)

0 Members and 1 Guest are viewing this topic.

Offline FlyinC4T

  • Newbie
  • *
  • Posts: 1
  • Karma: 0
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
« Last Edit: February 15, 2022, 09:15:14 AM by FlyinC4T »

Offline DerMetelGamerYT

  • Newbie
  • *
  • Posts: 1
  • Karma: 0
Re: Changing the global gravity (for players) with Entity:SetGravity() ?
« Reply #1 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)

Offline Codingale

  • Newbie
  • *
  • Posts: 4
  • Karma: 1
Re: Changing the global gravity (for players) with Entity:SetGravity() ?
« Reply #2 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

Offline iViscosity

  • Respected Community Member
  • Hero Member
  • *****
  • Posts: 802
  • Karma: 58
Re: Changing the global gravity (for players) with Entity:SetGravity() ?
« Reply #3 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 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).
« Last Edit: July 12, 2022, 12:00:05 PM by iViscosity »
I'm iViscosity. I like gaming and programming. Need some help? Shoot me PM.