• Someone to just explain this script ?
    5 replies, posted
[I'm french and I apologize if I'm forced to use gogole translation] **MY PROJECT** Hello and here I plan to make a metal pickup system on the ground, with a text showing on the hud with the number of pieces of metal ** MY PROBLEM** My problem and that I have a script that I found on this forum but I do not understand aps tosu and I do not want to make copied paste and see if its function i really want to know how it has done that c'ets Perfectly what I need but I would like to take no jsute do copied and paste C for its that I ask you to explain how this thing works [CODE]local Player = FindMetaTable("Player") if !Player then return end if SERVER then AddCSLuaFile("autorun/HungerMod.lua") util.AddNetworkString("Player_SendHunger") function Player:SetHunger(num) net.Start("Player_SendHunger") net.WriteInt(num, 16) net.Send(self) self.Hunger = num end function Player:AddHunger(num) self:SetHunger(self:GetHunger() + num) end function Player:HungerDecrease(amt,delay) self:HungerDecreaseStop() local time = delay or 30 timer.Create("Hunger_Decrease"..self:UniqueID(), time, 0, function() if self:GetHunger() > 0 then self:AddHunger(amt) else self:TakeDamage(15, self, self) end end) end function Player:HungerDecreaseStop() if timer.Exists("Hunger_Decrease"..self:UniqueID()) then timer.Remove("Hunger_Decrease"..self:UniqueID()) end end hook.Add("PlayerInitialSpawn", "Hunger.PlayerInitialSpawn", function(ply) if IsValid(ply) then timer.Simple(1, function() ply:SetHunger(100) ply:HungerDecrease(-1,10) end) end end) hook.Add("PlayerSpawn", "Hunger.PlayerSpawn", function(ply) ply:SetHunger(100) end) else net.Receive("Player_SendHunger", function(len) local num = net.ReadInt(16) LocalPlayer().Hunger = num end) hook.Add("HUDPaint", "Hunger.HUDPaint", function() local ply = LocalPlayer() local hunger = ply:GetHunger() local status = ply:HungerStatus() if IsValid(ply) and hunger and status then draw.SimpleText(hunger, "Default", 10, 10, Color(255,255,255,255)) draw.SimpleText(status, "Default", 10, 30, Color(255,255,255,255)) end end) end local Hunger_Status = { {100, "Full"}, {90, "Stuffed"}, {80, "Satisfied"}, {70, "Getting Hungry"}, {60, "Needs Food"}, {50, "Starving"}, {40, "Will eat anything"}, {20, "Dying"}, {0, "Gonna pass out"} } function Player:HungerStatus() local amt = self:GetHunger() for k,v in pairs(Hunger_Status) do if amt >= v[1] then return v[2] end end end function Player:GetHunger() return self.Hunger or 0 end[/CODE] Thank you to all those who could help me, really thank you very much and thanks Brandonj4 ;)
Was bored and commented it all for you.. But really.. Do not use this code. It's very badly written.. [CODE]--Saving a player metatable used to create functions that can be called on players. local Player = FindMetaTable("Player") --Stop the script from running further if we failed to create the metatable. if !Player then return end --Check if the code is running on the server if SERVER then --Tell the client to download autorun/HungerMod.lua AddCSLuaFile("autorun/HungerMod.lua") --Pool a networkmessage called Player_SendHunger. (Allow it to be sent/received between the client and the server) util.AddNetworkString("Player_SendHunger") --Create a function to update a players hunger by sending it from the server to the client. function Player:SetHunger(num) --Start netMsg net.Start("Player_SendHunger") --Send the hunger of the player from the first argument of the function. (16 bits) net.WriteInt(num, 16) --Send it to the player the function is called on. net.Send(self) --Set the hunger to the first given argument serverside. (So the server keep track of it) self.Hunger = num end --Add a function to add hunger to a player. function Player:AddHunger(num) --call the SetHunger function and GetHunger function and add the first argument. (Basically: Hunger + ToAdd = NewHunger) self:SetHunger(self:GetHunger() + num) end --Add a function to create a timer that will remove hunger from the player. Time is set depending on the second argument of the function or 30 if no argument is given. function Player:HungerDecrease(amt,delay) --Remove the current timer if this function already was called once. self:HungerDecreaseStop() --Check if the delay argument was given and use 30 instead if it wasn't. local time = delay or 30 --Create a forever timer that will remove food. timer.Create("Hunger_Decrease"..self:UniqueID(), time, 0, function() --If food is over 0 then remove as much as given in the first function argument. Otherwise: Take 15 damage. if self:GetHunger() > 0 then self:AddHunger(amt) else self:TakeDamage(15, self, self) end end) end --Add a function to remove the timer that removes your hunger. function Player:HungerDecreaseStop() --Check if the timer exists and remove it if it does. if timer.Exists("Hunger_Decrease"..self:UniqueID()) then timer.Remove("Hunger_Decrease"..self:UniqueID()) end end --Create a hook that will be called the first time a player spawns. hook.Add("PlayerInitialSpawn", "Hunger.PlayerInitialSpawn", function(ply) --Check if the player who spawned is valid. if IsValid(ply) then --Create a 1 second timer and then set the hunger to 100 and start a timer that removes 1 hunger every 10th second by calling the HungerDecrease() function. timer.Simple(1, function() ply:SetHunger(100) ply:HungerDecrease(-1,10) end) end end) --Create another hook that will be called EVERY time a player spawns which will reset the hunger of the player. hook.Add("PlayerSpawn", "Hunger.PlayerSpawn", function(ply) --You know what this function does now I hope? ply:SetHunger(100) end) else --If the code is running clientside now.. --Create a network receiver on the client that gets the hunger that the player has from the server. net.Receive("Player_SendHunger", function(len) --Read the sent hunger. (16 bit) local num = net.ReadInt(16) --Save it in a playervar.. (wtf.. lol) LocalPlayer().Hunger = num end) --Create a HUDPaint function that will draw the players hunger on the screen. hook.Add("HUDPaint", "Hunger.HUDPaint", function() --Save the local playerentity, the hunger and the hungerstatus in vars. (Micro optimization) (HungerStatus function is below) local ply = LocalPlayer() local hunger = ply:GetHunger() local status = ply:HungerStatus() --Check so all vars exists and that the player is valid. if IsValid(ply) and hunger and status then --Draw the hunger and the hungerstatus on the screen. draw.SimpleText(hunger, "Default", 10, 10, Color(255,255,255,255)) draw.SimpleText(status, "Default", 10, 30, Color(255,255,255,255)) end end) end --Create a table with words that will say how hungry you are. local Hunger_Status = { {100, "Full"}, {90, "Stuffed"}, {80, "Satisfied"}, {70, "Getting Hungry"}, {60, "Needs Food"}, {50, "Starving"}, {40, "Will eat anything"}, {20, "Dying"}, {0, "Gonna pass out"} } --Create a function to get the HungerStatus of a player. (Basically: Hunger to Text) (As above) function Player:HungerStatus() --Save the hunger in a local var. (Not even needed.. wtf lol) local amt = self:GetHunger() --Loop through all hungerstatuses and choose the right one depending on how much hunger you currently have. for k,v in pairs(Hunger_Status) do if amt >= v[1] then --Return the HungerStatus text to where it was called from. return v[2] end end end --Return the hunger of the player or 0 if no hunger is saved. function Player:GetHunger() return self.Hunger or 0 end[/CODE]
Ohhh thank you very much for this magnificent description <3 <3 <3 and also in relation to the fact that it is badly written that voullais vosu say because I will not use timer just the function addMetal [LUA]local Player = FindMetaTable("Player") if !Player then return end if SERVER then AddCSLuaFile("autorun/metal.lua") util.AddNetworkString("Player_SendMetal") function Player:SetMetal(num) net.Start("Player_SendMetal") net.WriteInt(num, 16) net.Send(self) self.Hunger = num end function Player:AddMetal(num) self:SetHunger(self:GetHunger() + num) end hook.Add("PlayerInitialSpawn", "Metal.PlayerInitialSpawn", function(ply) if IsValid(ply) then timer.Simple(1, function() ply:SeMetal(0) end) end end) hook.Add("PlayerSpawn", "Metal.PlayerSpawn", function(ply) ply:SetMetal(0) end) else net.Receive("Player_SendMetal", function(len) local num = net.ReadInt(16) LocalPlayer().Metal = num end) hook.Add("HUDPaint", "Metal.HUDPaint", function() local ply = LocalPlayer() local metal = ply:GetMetal() if IsValid(ply) and hunger then draw.SimpleText(hunger, "DermaLarge", 10, 10, Color(255,255,255,255)) end end) end function Player:GetHunger() return self.Metal or 0 end[/LUA] this is my version with any timer
What I should have done is: (NOTE LINES MAY JUMP AROUND AS YOU CHANGE STUFF) - Remove line 2. (We don't need to check if we created it or not) - Remove the PlayerInitialSpawn hook. The PlayerSpawn hook is called on first spawn anyways. - Line 31: Remove the num var and read it directly as it's only needed once. - Line 40 + 41: You are still using the no longer existing Hunger var instead of the new metal var. - Also I wouldn't use this way of networking probably. It works and it's not bad but there are more convenient ways of doing it. For example you could use NWVars which are slow and should be avoided, but they will soon be updated and I don't think you will see a bigger issue using them. You could also look into using [URL="https://wiki.garrysmod.com/page/Entity/NetworkVar"]NetworkVars[/URL] on players which is an alot better alternative than NWVars (But may be trickier).
[QUOTE=Jompe;52479468]You could also look into using NetworkVars on players which is an alot better alternative than NWVars (But may be trickier).[/QUOTE] NetworkVars are just DTVars internally, and essentially match NW2Vars for entities inside another player's PVS. For this script, I would say the net library is fine since it only needs to be synced to the local player -- DTVars, NWVars, and NW2Vars will sync their value with multiple players.
[QUOTE=Jompe;52479468]What I should have done is: (NOTE LINES MAY JUMP AROUND AS YOU CHANGE STUFF) - Remove line 2. (We don't need to check if we created it or not) - Remove the PlayerInitialSpawn hook. The PlayerSpawn hook is called on first spawn anyways. - Line 31: Remove the num var and read it directly as it's only needed once. - Line 40 + 41: You are still using the no longer existing Hunger var instead of the new metal var. - Also I wouldn't use this way of networking probably. It works and it's not bad but there are more convenient ways of doing it. For example you could use NWVars which are slow and should be avoided, but they will soon be updated and I don't think you will see a bigger issue using them. You could also look into using [URL="https://wiki.garrysmod.com/page/Entity/NetworkVar"]NetworkVars[/URL] on players which is an alot better alternative than NWVars (But may be trickier).[/QUOTE] Thank you so much for your help guys <3 [editline]17th July 2017[/editline] [QUOTE=code_gs;52479553]NetworkVars are just DTVars internally, and essentially match NW2Vars for entities inside another player's PVS. For this script, I would say the net library is fine since it only needs to be synced to the local player -- DTVars, NWVars, and NW2Vars will sync their value with multiple players.[/QUOTE] OK to what I understood the NWVAR will not adapt to this type of use ?
Sorry, you need to Log In to post a reply to this thread.