[lua]
if SERVER then
hook.Add("OnNPCKilled", "datzomkill", function( npc, killer, weapon )
if killer:IsPlayer() then
if killer:GetPData("TZK") == nil then
MsgN("Found Nil Value, Atempting To Bypass.")
killer:SetPData("TZK", 0 )
end
killer:SetPData("TZK", killer:GetPData("TZK") + 1 )
end
end)
end
function hud()
local bleh = LocalPlayer():GetPData("TZK", 0)
draw.RoundedBox( 4, 2, 2, ScreenScale(639),23, Color(50,50,50,50) )
draw.SimpleText( "MPhx - F1:Help | F2:Stop Music | F3:ScanMenu | F4:FactionMenu", "Trebuchet18", 2, 2, Color(255,255,255,255), TEXT_ALIGN_LEFT)
draw.SimpleText( "Total Zombies Killed: " .. bleh, "Trebuchet18", ScreenScale(639), 2, Color(255,255,255,255), TEXT_ALIGN_RIGHT)
end
hook.Add("HUDPaint", "MyHudName", hud)
[/lua]
i have looked at the database and it is adding 1 to TZK but the hud is not displaying the number all im getting is nil or 0 depending on if i have the 0 in local bleh = LocalPlayer():GetPData("TZK", 0)
been trying to figure this out for 3 hours now and iv just about given up. im a pretty much a big box when it comes to lua XD
No Errors
[t]http://puu.sh/8ddk2.png[/t]
That's not very informative i understand im trying to use server functions and local functions. how would i go about passing information between the 2??
of course i probably have this all wrong in my head
It's explaining what you're doing wrong. You're trying to draw it on the HUD ([b]client[/b]) with a ([b]server[/b]) function.
If you're trying to count their kills, then what you can do is each kill network it to the player. So for example I do this with damage:
[b]Server[/b]
[lua]-- Sets the players damage --
function plyMeta:SetDamage(dmg)
if self:IsValid() and self:IsPlayer() then
self.Dmg = dmg
net.Start("gw_updatedamage")
net.WriteInt(self.Dmg, 32)
net.Send(self)
end
end[/lua]
Then on the receiving end...
[b]Client[/b]
[lua]net.Receive("gw_updatedamage", function(len, CLIENT)
damage = net.ReadInt(32)
end)[/lua]
damage is a global variable on the clientside which I draw to the player.
im trying to count their kills and keep the information for the next time they join while also passing that number to the client side function
So try this.
[lua]if SERVER then
util.AddNetworkString("tzk_updatekills")
hook.Add("OnNPCKilled", "datzomkill", function( npc, killer, weapon )
if killer:IsPlayer() then
if killer:GetPData("TZK") == nil then
MsgN("Found Nil Value, Atempting To Bypass.")
killer:SetPData("TZK", 0 )
end
killer:SetPData("TZK", killer:GetPData("TZK") + 1 )
net.Start("tzk_updatekills")
net.WriteInt(killer:GetPData("TZK"), 32)
net.Send(killer)
end
end)
else
local kills = 0
net.Receive("tzk_updatekills", function(len, CLIENT)
kills = net.ReadInt(32)
end)
function hud()
local bleh = LocalPlayer():GetPData("TZK", 0)
draw.RoundedBox( 4, 2, 2, ScreenScale(639),23, Color(50,50,50,50) )
draw.SimpleText( "MPhx - F1:Help | F2:Stop Music | F3:ScanMenu | F4:FactionMenu", "Trebuchet18", 2, 2, Color(255,255,255,255), TEXT_ALIGN_LEFT)
draw.SimpleText( "Total Zombies Killed: " .. kills, "Trebuchet18", ScreenScale(639), 2, Color(255,255,255,255), TEXT_ALIGN_RIGHT)
end
hook.Add("HUDPaint", "MyHudName", hud)
end[/lua]
THANK YOU! lol now to pick this apart and try to figure out how exactly it works for the next time i want to do something like this
All I did was add networking. Whenever you need the client to have some sort of info the server has, or if the client needs to send it (becareful of this, never trust them and assume the worst, so many checks) you can use this.
awesome. i think i understand it after looking over it and connecting the dots. would the "bitCount" ever need to be higher then 32?
[QUOTE=kulcris;44576539]awesome. i think i understand it after looking over it and connecting the dots. would the "bitCount" ever need to be higher then 32?[/QUOTE]
32 bits has 2^31 possibilities, so no.
You don't need 32 bits, its better to use 16 bits but its such a small amount it doesn't matter.
[QUOTE=Nookyava;44576448]So try this.
[lua]if SERVER then
util.AddNetworkString("tzk_updatekills")
hook.Add("OnNPCKilled", "datzomkill", function( npc, killer, weapon )
if killer:IsPlayer() then
if killer:GetPData("TZK") == nil then
MsgN("Found Nil Value, Atempting To Bypass.")
killer:SetPData("TZK", 0 )
end
killer:SetPData("TZK", killer:GetPData("TZK") + 1 )
net.Start("tzk_updatekills")
net.WriteInt(killer:GetPData("TZK"), 32)
net.Send(killer)
end
end)
else
local kills = 0
net.Receive("tzk_updatekills", function(len, CLIENT)
kills = net.ReadInt(32)
end)
function hud()
local bleh = LocalPlayer():GetPData("TZK", 0)
draw.RoundedBox( 4, 2, 2, ScreenScale(639),23, Color(50,50,50,50) )
draw.SimpleText( "MPhx - F1:Help | F2:Stop Music | F3:ScanMenu | F4:FactionMenu", "Trebuchet18", 2, 2, Color(255,255,255,255), TEXT_ALIGN_LEFT)
draw.SimpleText( "Total Zombies Killed: " .. kills, "Trebuchet18", ScreenScale(639), 2, Color(255,255,255,255), TEXT_ALIGN_RIGHT)
end
hook.Add("HUDPaint", "MyHudName", hud)
end[/lua][/QUOTE]
"local bleh = LocalPlayer():GetPData("TZK", 0)" is completely pointless, you can't use GetPData clientside.
I didn't notice the bleh at all *facepalms*
lol i noticed it after you posted this and just removed it from the code you gave me. thanks again for all your help
so 32 would be a max number of 4294967296? 2^32?
[QUOTE=kulcris;44576896]lol i noticed it after you posted this and just removed it from the code you gave me. thanks again for all your help
so 32 would be a max number of 4294967296? 2^32?[/QUOTE]
Not in this case. It would be 2^31 (about 2.14 billion), whilst reserving the leading bit ( in the case of a little endian chip ) for the sign value, basically telling the processor if the number is positive or negative.
If you want to use all 32 bits for the value of the number and ignore the sign, this is called an unsigned integer. The direct result of this is that unsigned integers can only be positive, as they don't have a sign value. For a value that will never be negative, you might want to network unsigned numbers, as you can pack more information into a slightly smaller space. It would look like this:
[CODE]
if SERVER then
//remeber that newworked strings must be 'initialized' on the server at least a couple seconds before they're used.
//they simply will not work otherwise. You'll get an error message like 'attempting to use unpooled message' if you've done it wrong.
util.AddNetworkString("tzk_updatekills")
hook.Add("OnNPCKilled", "datzomkill", function( npc, killer, weapon )
if killer:IsPlayer() then
//the second argument of GetPData is the default value should the search return no data.
//no need to query the sqlite DB more than needed. While SQLite is quick, accessing it more than needed is still an unnecessary slowdown.
local cur_kills = killer:GetPData( "TZK", 0 )
killer:SetPData("TZK", cur_kills + 1 )
//an unsigned integer of 16 bits will hold a maximum value of 65536, and a minimum of 0.
//since I highly doubt that anyone will hit anywhere close to that value, you might as well
//network your data just a little bit quicker.
net.Start("tzk_updatekills")
net.WriteUInt( cur_kills+1, 16 )
net.Send(killer)
end
end )
else //only two possibilies exist. Server or Client
//here we intitialize an upvalue so that when we read it in from our net.Receive we can keep it defined outside of that scope.
//if we didn't do that, then the variable 'kills' would not befined when our HUDPaint hook tried to access it
local kills = 0
//here, len is the length of the net message in bits. I've only used it a couple times, and that's when I was networking large strings and had to worry about my data limit.
//usually you can ignore it.
net.Receive("tzk_updatekills", function( len )
kills = net.ReadUInt(16)
end)
//keep in mind that the HUDPaint hook is called once every client frame. Nothing here was wrong, just remiding you that you can accidentally cause
//major lag if you do something like create a font or load a texture inside of a HUDPaint (or similar) hook.
hook.Add("HUDPaint", "MyHudName", function()
draw.RoundedBox( 4, 2, 2, ScreenScale(639),23, Color(50,50,50,50) )
draw.SimpleText( "MPhx - F1:Help | F2:Stop Music | F3:ScanMenu | F4:FactionMenu", "Trebuchet18", 2, 2, Color(255,255,255,255), TEXT_ALIGN_LEFT)
draw.SimpleText( "Total Zombies Killed: " .. kills, "Trebuchet18", ScreenScale(639), 2, Color(255,255,255,255), TEXT_ALIGN_RIGHT)
end
end
[/CODE]
I changed some of the previous code and commented it a bit to hopefully help you understand a little better.
Sorry, you need to Log In to post a reply to this thread.