0

To clarify, because it was hard to explain in the title.

-- file that contains this code is > "player.lua"

local ply = FindMetaTable("Player")

ply.LastDamageType = "N/A"

this code sets a new variable to all player entities. The variable is just a string of the last entity that did damage to the player. That is made possible by the following code:

-- file that contains this code is > "init.lua"

AddCSLuaFile("shared.lua")
AddCSLuaFile("cl_init.lua")

include("shared.lua")
include("player.lua")

-->
--> OTHER METHODS THAT I DIDN'T INCLUDE
-->

function GM:PlayerShouldTakeDamage(ply, attacker)

    ply.LastDamageType=tostring(attacker)

    return true

end

My point of doing all this is because I want to have a hud element that will display this string so the client can see what was the last thing that dealt damage to him.

The problem is that, the variable is coming up as nil on the client side file "cl_init", yet if I printed the table that contained all the data that was in the player through the client AND through the server side. both tables were the same excluding the variable I made "LastDamageType"

-- file that contains this code is > "cl_init.lua"

include("shared.lua")

surface.CreateFont("dayz_font",{font="Arial",size=24,weight=400})

function GM:HUDPaint()

    local ply = LocalPlayer()
    draw.SimpleText("you are "..ply:Nick(),"dayz_font", 150,10,Color(255,120,50),TEXT_ALIGN_CENTER)//TargetID

    surface.SetFont("TargetID")
    surface.SetTextColor(Color(30,70,130))
    surface.SetTextPos(surface.ScreenWidth()-200 ,20)
    surface.DrawText("You have  ")
    surface.SetTextColor(Color(200,90,30))
    surface.DrawText(ply:Health())
    surface.SetTextColor(Color(30,70,130))
    surface.DrawText(" health")

    surface.SetTextPos(surface.ScreenWidth()-200 ,35)
    surface.DrawText(""..tostring(Entity(1):GetTable().LastDamageType))
    -- FOR DEBUGGING PURPOSES, I'M USING "Entity(1)" TO GET MY PLAYER
    -- I KNOW THAT "LocalPlayer()" PROBABLY ALSO EQUATES TO THE SAME THING
    -- AS WHAT "Entity(1)" IS AT THIS MOMENT BECAUSE I'M TESTING THIS IN SINGLEPLAYER.

end

So it seems that the player data on the client side and the server side are not the SAME thing and that they have to be synchronized or something like that... Or did I miss something important that kept them synchronized?

////////////////////////////////////////////////// ADDITIONAL INFORMATION //////////////////////////////////////////////////

These are printed lists of all the variables of my player entity when I did

PrintTable(Entity(1):GetTable())

on both the client side and server side. The first list is on the server and the second is on the client. I will separate them with ==========================

I know you don't probably want to look through the list to just to Ctrl+F and type in "LastDamageType" to highlight it.

///////////////////////////////////////// SERVER SIDE

CWAttachments:
    am_flechetterounds  =   true
    am_magnum   =   true
    am_matchgrade   =   true
    am_slugrounds   =   true
    bg_ak74_rpkbarrel   =   true
    bg_ak74_ubarrel =   true
    bg_ak74foldablestock    =   true
    bg_ak74heavystock   =   true
    bg_ak74rpkmag   =   true
    bg_ar1560rndmag =   true
    bg_ar15heavystock   =   true
    bg_ar15sturdystock  =   true
    bg_bipod    =   true
    bg_deagle_compensator   =   true
    bg_deagle_extendedbarrel    =   true
    bg_foldsight    =   true
    bg_longbarrel   =   true
    bg_longbarrelmr96   =   true
    bg_longris  =   true
    bg_magpulhandguard  =   true
    bg_mp530rndmag  =   true
    bg_mp5_kbarrel  =   true
    bg_mp5_sdbarrel =   true
    bg_nostock  =   true
    bg_regularbarrel    =   true
    bg_retractablestock =   true
    bg_ris  =   true
    bg_sg1scope =   true
    md_acog =   true
    md_aimpoint =   true
    md_anpeq15  =   true
    md_cobram2  =   true
    md_eotech   =   true
    md_foregrip =   true
    md_kobra    =   true
    md_m203 =   true
    md_microt1  =   true
    md_pbs1 =   true
    md_pso1 =   true
    md_saker    =   true
    md_tundra9mm    =   true
CalcIdeal   =   990
CalcSeqOverride =   -1
DTVar   =   function: 0x1ca93680
EditValue   =   function: 0x2ac6c878
GetEditingData  =   function: 0x18501238
GetNetworkKeyValue  =   function: 0x1caf7ec0
GetNetworkVars  =   function: 0x1caf9640
LastDamageType  =   Entity [0][worldspawn]
LastPlayerTrace =   859.94995117188
LastSpawnpoint  =   Entity [61][info_player_start]
NetworkVar  =   function: 0x2375b100
NetworkVarElement   =   function: 0x23785a08
NetworkVarNotify    =   function: 0x1844e3c8
PlayerTrace:
    Entity  =   Entity [0][worldspawn]
    Fraction    =   0.49089023470879
    FractionLeftSolid   =   0
    Hit =   true
    HitBox  =   0
    HitGroup    =   0
    HitNoDraw   =   false
    HitNonWorld =   false
    HitNormal   =   -1.000000 0.000000 0.000000
    HitPos  =   15359.968750 950.995850 -12563.697266
    HitSky  =   true
    HitTexture  =   TOOLS/TOOLSSKYBOX
    HitWorld    =   true
    MatType =   88
    Normal  =   0.998662 0.047185 -0.021163
    PhysicsBone =   0
    StartPos    =   -704.000000 192.000000 -12223.280273
    StartSolid  =   false
    SurfaceProps    =   77
RestoreNetworkVars  =   function: 0x184b21a8
SCarMouseMoveX  =   0
SCarMouseMoveY  =   0
SetNetworkKeyValue  =   function: 0x1838a3f8
SetupEditing    =   function: 0x0f107ab8
SetupKeyValue   =   function: 0x1844da70
WT_RagdollRoper_Roping  =   false
canChat =   864.94995117188
dt:
m_CurrentPlayerClass:
    ClassID =   125
    Func    =   function: 0x1ca70b58
    Player  =   Player [1][UbErZ Andrew900460]
m_bFlashlight   =   true
m_bInSwim   =   false
m_bWasNoclipping    =   false
m_bWasOnGround  =   true

//////////////////////////////////////////////////////////////// CLIENT SIDE

CWAttachments:
    am_flechetterounds  =   true
    am_magnum   =   true
    am_matchgrade   =   true
    am_slugrounds   =   true
    bg_ak74_rpkbarrel   =   true
    bg_ak74_ubarrel =   true
    bg_ak74foldablestock    =   true
    bg_ak74heavystock   =   true
    bg_ak74rpkmag   =   true
    bg_ar1560rndmag =   true
    bg_ar15heavystock   =   true
    bg_ar15sturdystock  =   true
    bg_bipod    =   true
    bg_deagle_compensator   =   true
    bg_deagle_extendedbarrel    =   true
    bg_foldsight    =   true
    bg_longbarrel   =   true
    bg_longbarrelmr96   =   true
    bg_longris  =   true
    bg_magpulhandguard  =   true
    bg_mp530rndmag  =   true
    bg_mp5_kbarrel  =   true
    bg_mp5_sdbarrel =   true
    bg_nostock  =   true
    bg_regularbarrel    =   true
    bg_retractablestock =   true
    bg_ris  =   true
    bg_sg1scope =   true
    md_acog =   true
    md_aimpoint =   true
    md_anpeq15  =   true
    md_cobram2  =   true
    md_eotech   =   true
    md_foregrip =   true
    md_kobra    =   true
    md_m203 =   true
    md_microt1  =   true
    md_pbs1 =   true
    md_pso1 =   true
    md_saker    =   true
    md_tundra9mm    =   true
CalcIdeal   =   990
CalcSeqOverride =   -1
ChatGestureWeight   =   0
DTVar   =   function: 0x2be3b410
EditValue   =   function: 0x3053c750
GetEditingData  =   function: 0x2bb01b30
GetNetworkKeyValue  =   function: 0x2bb01d18
GetNetworkVars  =   function: 0x2bb01d38
LastPlayerTrace =   859.96203613281
NetworkVar  =   function: 0x2bb01c98
NetworkVarElement   =   function: 0x2bab5340
NetworkVarNotify    =   function: 0x2bb01cd8
PlayerTrace:
    Entity  =   Entity [0][worldspawn]
    Fraction    =   0.49089023470879
    FractionLeftSolid   =   0
    Hit =   true
    HitBox  =   0
    HitGroup    =   0
    HitNoDraw   =   false
    HitNonWorld =   false
    HitNormal   =   -1.000000 0.000000 0.000000
    HitPos  =   15359.968750 950.995850 -12563.697266
    HitSky  =   true
    HitTexture  =   TOOLS/TOOLSSKYBOX
    HitWorld    =   true
    MatType =   88
    Normal  =   0.998662 0.047185 -0.021163
    PhysicsBone =   0
    StartPos    =   -704.000000 192.000000 -12223.280273
    StartSolid  =   false
    SurfaceProps    =   77
RestoreNetworkVars  =   function: 0x2bb01d58
SetNetworkKeyValue  =   function: 0x2bb01cf8
SetupEditing    =   function: 0x2bb01c38
SetupKeyValue   =   function: 0x2bb01c58
dt:
m_CurrentPlayerClass:
    ClassID =   125
    Func    =   function: 0x30283ff8
    Player  =   Player [1][UbErZ Andrew900460]
m_bInSwim   =   false
m_bWasNoclipping    =   false
m_bWasOnGround  =   true
Andrew900460
  • 621
  • 1
  • 6
  • 16
  • "So it seems that the player data on the client side and the server side are not the SAME thing and that **they have to be synchronized**" Well, yes. If you have two different Lua states running in different processes and you or the engine you're using is not synchronizing them, then how would they even know the other exists? – Mud Aug 12 '15 at 19:48
  • ok well, I just need to know how to "copy/paste" the server's player data to the client. – Andrew900460 Aug 12 '15 at 19:53
  • Maybe start [here](http://wiki.garrysmod.com/page/Net_Library_Usage)? I just Googled "gary's mod client server lua". I don't know enough about that mod or your code to tell if you that's the best approach, but if Gary's mod lets you run Lua code on both the server and client and it doesn't automatically synchronize them (through some kind of voodoo magic), then it's up to you to do that. – Mud Aug 12 '15 at 19:58

1 Answers1

0

If you wish to have a network shared variable that you can assign to an entity (e.g. a player) you want to use NWVariables. In your situation, since you are saving an entity you want to look at SetNWEntity and GetNWEntity.

Here is how you would implement it in the PlayerShouldTakeDamage hook:

function GM:PlayerShouldTakeDamage(ply, attacker)

    ply:SetNWEntity("LastDamageType", attacker)

    return true

end
StarFox
  • 132
  • 9