• 2 Problems with 1 Variable
    3 replies, posted
I have a variable set which decreases by 1 every second. I also have an entity which when it makes contact with another entity, disappears and adds 17 to the variable. However, the variable doesn't add it properly. It adds it 2-3 times. Also, when I try to take a variable from init.lua and put it into cl_init.lua (I tried doing it with and without networking), it doesn't read the variable. It gives an error when I have it set to display on a world tip and when I use tostring on it. And finally, "if var < 1" doesn't register. It keeps going under 1 into the negatives. [lua] var = 100 timer.Create("Timer", 1, 0, function() var = var - 1 for k,v in pairs(player.GetAll()) do v:PrintMessage(HUD_PRINTTALK, var) end end) if var < 1 then self:Remove() timer.Destroy("T") end[/lua] [LUA] if hitEnt:GetClass() == "test_entity" and self:GetNWBool("Bool") == false then hitEnt:Remove() var = var + 17 timer.Destroy("Timer") for k,v in pairs(player.GetAll()) do v:PrintMessage(HUD_PRINTTALK, var) end end[/LUA] [lua] if self:BeingLookedAtByLocalPlayer() then AddWorldTip(self:EntIndex(), "Test\nOwner: "..LocalPlayer():Nick().."\nVar: "..var, 0.5, self:GetPos(), self) end[/lua] The error it gives me when I look at the entity: [lua][ERROR] addons/addon/lua/entities/test/cl_init.lua:13: attempt to concatenate global 'var' (a nil value) 1. unknown - addons/addon/lua/entities/test/cl_init.lua:13 2. DrawModel - [C]:-1 3. Render - lua/includes/modules/halo.lua:70 4. fn - lua/includes/modules/halo.lua:168 5. unknown - addons/ulib/lua/ulib/shared/hook.lua:183 [/lua]
init.lua is SERVERside while cl_init.lua is CLIENTside. Creating a var, even if it was a global, wouldn't work like that... Put it in the SHARED file if you want to do that; but even then, the client and server "connect" at different times, so you'd need to send a net-message to all connecting players to let them know where the var was currently... You could also use NWVar ( They can cause issues, so I'd recommend caching, check for a new number every half a second to second since it won't change more often than that ) Simple networking tutorial: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/networking/networking_booleans.lua.html[/url] Not good tests but some shows that nwvar can be costly: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/benchmarking_tips/benchmarking_nw_vars.lua.html[/url] Caching example: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/making_players_invisible_and_caching_variables.lua.html[/url]
Didn't work :L same error init [lua] util.AddNetworkString("Net") net.Start("Net") net.WriteString(var) net.Broadcast()[/lua] cl_init [lua] net.Receive("Net", function(len) var = net.ReadString() end)[/lua] EDIT: I spawned the entities again and the var is changing but the displayed var is still stuck at 100.
I wrote up a quick tutorial for count-down: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/vgui/sh_hud_countdown.lua.html[/url]
Sorry, you need to Log In to post a reply to this thread.