I'm working with Network Strings and I'm trying to make a GUI that buffs a gun's stats.
I've made a simple Derma Panel that shows up when F4 is pressed. The serverside goes as follows:
util.AddNetworkString("F4Menu")
function GM:ShowSpare2(plr)
net.Start("F4Menu")
net.Send(plr)
end
The clientside Derma Panel code:
net.Receive("F4Menu", function()
local Frame = vgui.Create("DFrame")
--Frame:SetPos(ScrW()/2-ScrW()/8, ScrH()/2-ScrH()/8)
Frame:SetSize(ScrW()/4, ScrH()/4)
Frame:Center()
Frame:SetTitle("Weapon Upgrades")
Frame:SetVisible(true)
Frame:SetDraggable(true)
Frame:ShowCloseButton(true)
Frame:MakePopup()
Frame:SetDeleteOnClose()
local DamageUpgrade = vgui.Create("DButton")
DamageUpgrade:SetParent(Frame)
DamageUpgrade:SetText("+15% Damage")
DamageUpgrade:SetPos(0.1*(ScrW()/8-60), ScrH()/8-25)
DamageUpgrade:SetSize(120, 50)
DamageUpgrade.DoClick = function()
net.Start("F4Menu")
net.WriteString("Damage")
net.SendToServer()
end
local FireUpgrade = vgui.Create("DButton")
FireUpgrade:SetParent(Frame)
FireUpgrade:SetText("+10% Fire Rate")
FireUpgrade:SetPos(ScrW()/8-60, ScrH()/8-25)
FireUpgrade:SetSize(120, 50)
FireUpgrade.DoClick = function()
net.Start("F4Menu")
net.WriteString("FireRate")
net.SendToServer()
end
end)
The serverside again:
net.Receive("F4Menu", function(len, plr)
--print(net.ReadString())
if net.ReadString() == "Damage" then
local defaultDmg = 20
local increase = defaultDmg * 0.15
plr:GetActiveWeapon().Damage = plr:GetActiveWeapon().Damage + increase
print("New Upgrade! Damage:", plr:GetActiveWeapon().Damage)
elseif net.ReadString() == "FireRate" then
print("test")
end
end)
I'm trying to send the information back to the server, and it works ... partially. When I click on the DamageUpgrade Button, it reads the string and properly does everything in the "if net.ReadString() == "Damage" then" block. But when pressing the FireUpgrade button, it does not print "test" to the console. It seems to only accept "Damage" as an input. Is this because the F4Menu cannot overwrite the previous string? I could add a new NetworkString for every single button, but that seems terribly inefficient. Any ideas or fixes?
Because you calling net.ReadString twice.
First time you called it in if then in elseif.
But when elseif is trying to get string it gets nothing.
When you net.ReadSomething and use it in several places in code use variables.
Make this in first line of serverside net.Receive:
local anwser = net.ReadString()
Thank you! Forgot that ReadString was a function being called and not simply a value.
Sorry, you need to Log In to post a reply to this thread.