Best way to make a counting system for every time a button is pressed

Hello!

I’ve been thinking about what would be the best way to make a number increase by 1 every time a DButton is pressed and then send this new number back to every client.

I’ve been able to brainstorm a few ideas but have had no luck. These are my most *sensible *ideas I’ve got so far:

I’ve been able to try a rough version of the idea on the left, but had no luck on completing it as I’ve drawn a blank. The monstrosity of my thought process turning into code after watching a very heated debate on Question Time was this:


 // The Button		
Join.DoClick = function ()
			net.Start("Imin")
				net.WriteInt(1)
			net.SendToServer()
			DermaBob:Remove()
		end

// The number changing
		Count.Think = function( nself )
		net.Receive("Imin")
			net.ReadString()
			nself:SetText( attendees )
		end

// Server
	util.AddNetworkString("Imin")
	local attendees = 0
	net.Receive("Imin")
		local Iminers = net.ReadInt()
		local attendees += Iminers


	net.Start("Imin")
		local net.WriteString(attendees)
	net.Broadcast()


I’m aware there are multiple disasters in this code such as me adding the two variables incorrectly or converting the integer into a string and the various other failures. Just hoping for an opinion on the best way to be doing it before I continue to attempt. No need for spoon feeding :slight_smile:

Big thanks in advance, (and sorry for my catastrophe of a brain storm)
Bings

For reference if anyone is intrested ive used (and tried using) as there relevant to what I’m doing
https://wiki.garrysmod.com/page/net/Broadcast
https://wiki.garrysmod.com/page/net/ReadInt
https://wiki.garrysmod.com/page/net/ReadString
https://wiki.garrysmod.com/page/net/Send
https://wiki.garrysmod.com/page/net/SendToServer
https://wiki.garrysmod.com/page/net/Start
https://wiki.garrysmod.com/page/net/WriteInt
https://wiki.garrysmod.com/page/net/Receive
https://wiki.garrysmod.com/page/net/WriteString

Untested, like this by any chance?


-- Client
local button = vgui.Create("DButton")
button:SetSize(ScrW() * 0.1, ScrH() * 0.1)
button:Center()
button.DoClick = function()
     net.Start("Imin")
     net.SendToServer()
end

net.Receive("broadcast_lele", function()
     local times = net.ReadUInt()
end)

--Server
util.AddNetworkString("Imin")

local times = 0

net.Receive("Imin", function()
	times = times + 1

	net.Start("broadcast_lele")
	net.WriteUInt(times, 32)
	net.Broadcast()
end)


Exactly like that!

So far I’ve eaten through a few errors, yet to fully get it working but didn’t want to leave you waiting!

Error:


[ERROR] lua/server.lua:30: Calling net.Start with unpooled message name! [http://goo.gl/qcx0y]
  1. Start - [C]:-1
   2. func - lua/server.lua:30
    3. unknown - lua/includes/extensions/net.lua:32

Fix:


	util.AddNetworkString("broadcast_lele") // just adding this to the server file, ezy pzy

Error:


[ERROR] lua/client.lua:192: bad argument #1 to 'SetText' (string expected, got nil)
  1. SetText - [C]:-1
   2. unknown - lua/client.lua:192

Temporary Fix so I could get the next error


		local times = 0 // Added this
		net.Receive("broadcast_lele", function()
    		local times = net.ReadUInt()
			end)
			nself:SetText( times )
		end


Error I’m working on:


[ERROR] lua/client.lua:191: bad argument #1 to 'ReadUInt' (number expected, got no value)
  1. ReadUInt - [C]:-1
   2. func - lua/client.lua:191
    3. unknown - lua/includes/extensions/net.lua:32

Will edit when fixed

Big thanks,
Bings :slight_smile:

Your SetText error is because times doesnt exist yet.
Do something like:


if IsValid(times) then button:SetText(times) end

As for the other error please post all your updated code.

Was prepping a big response with what I’ve done and test so far, but it got long and complicated. But to shrink it down I tested to see if the server file was acting properly, it was. It was correctly counting the amount of times the button was pressed and sending it back.

Then back over to the client side I found that for some reason it wasn’t receiving the new value.

As for the code, here’s all the relevant stuff at its most functional state at the moment:


// The button	

local Join = vgui.Create( "DButton", DermaBob)
		Join:SetPos(210,160)
		Join:SetSize(70,30)
		Join:SetText("")
		Join.Paint = function (s, w, h)
			draw.RoundedBoxEx(4,0,0,w,h, Color(80,230,45),true,true,true,true)
			draw.SimpleText("Yes please","BaeSmall",0+6,0+5,Color(255,255,255),TEXT_ALIGN_LEFT,TEXT_ALIGN_TOP)
		end
		Join.DoClick = function ()
     	net.Start("Imin")
     	net.SendToServer()
			DermaBob:Remove()
		end


// The counting part	

local PlayerCount = vgui.Create ("DLabel", DermaBob)
	    PlayerCount(440,162)
	    PlayerCount( 64 )
	   	PlayerCount (Color(0,0,0,255))
		PlayerCount("Bae")
		PlayerCount.Think = function( nself )
		local times = 0
		net.Receive("broadcast_lele", function()
    		local times = net.ReadUInt()
			print("Recieved "..times) // This doesn't happen, error is on the line above
			end)
			nself:SetText( times )
		end


// The server

util.AddNetworkString("Imin")
util.AddNetworkString("broadcast_lele")

local times = 0

net.Receive("Imin", function()
	times = times + 1

	net.Start("broadcast_lele",false)
	net.WriteUInt(times, 32)
	net.Broadcast()
	print("I worked! I've broadcasted "..times) // This works
end)

Far as my simple brain can tell, its either that its Broadcasting the number but its not reaching its destination, or that it doesn’t like the handling from the net message to a variable. Just my thoughts though.

change


net.ReadUInt()

to


net.ReadUInt(32)

I should really be testing these things in GMOD before helping you but I’ve been busy learning C++ today. Appologies for the mistake :slight_smile:

Let me know how it goes.

Edit: Just realised you’re the guy who replied to my thread the other day, thanks for that.

Hey, that fixed the issue of if it not recieving the number, but now why it’s just a case of the text not changing as its not setting the new number variable!

But thanks for that, Amazing help.

Thanks,
Bings

What you need to do is do the SetText inside the receive so when it receives the net message it will change the text.

I just found this thread so sorry for being late, but why not just change a serverside convar instead? I guess it doesn’t really make a difference, but at least you can get a convar’s value clientside without using the net library too much

Sadly it keeps throwing back


[ERROR] lua/client.lua:195: Tried to use invalid object (type Panel) (Object was NULL or not of the right type)
  1. SetText - [C]:-1
   2. func - lua/client.lua:195
    3. unknown - lua/includes/extensions/net.lua:32



Gonna look into a new way for it to display the number!

Edit:

Fixed, but the fix will be a screech to someone’s ears. It involved making times a global variable (ouch) but its the only way ive been able to get it working.

For future reference if anyone stumbles onto this page in need of help, the ifnished code was the follwing:

The button was


	local Join = vgui.Create( "DButton", DermaBob)
		Join:SetPos(210,160)
		Join:SetSize(70,30)
		Join:SetText("")
		Join.Paint = function (s, w, h)
			draw.RoundedBoxEx(4,0,0,w,h, Color(80,230,45),true,true,true,true)
			draw.SimpleText("Yes please","BaeSmall",0+6,0+5,Color(255,255,255),TEXT_ALIGN_LEFT,TEXT_ALIGN_TOP)
		end
		Join.DoClick = function ()
     	net.Start("Imin")
     	net.SendToServer()
			DermaBob:Remove()
		end

The server file was


util.AddNetworkString("Imin")
util.AddNetworkString("broadcast_lele")

local times = 0

net.Receive("Imin", function()
	times = times + 1

	net.Start("broadcast_lele",false)
	net.WriteUInt(times, 32)
	net.Broadcast()
end)

The updating label was:


	local PlayerCount= vgui.Create ("DLabel", DermaBob)
	    PlayerCount(440,162)
	    PlayerCount( 64 )
	   	PlayerCount(Color(0,0,0,255))
		PlayerCount("Bae")
		PlayerCount(0)
		PlayerCount.Think = function( nself )
		net.Receive("broadcast_lele", function()
    		times = net.ReadUInt(32)
			end)
		nself:SetText(times)
		end

Massive thank to John!

Bings :smile: