Trying to make var for client and server to access
6 replies, posted
I have been trying to fix this non stop for 2-3 days and I just cant seem to do it. I am trying to make a var where client and server can access and change.
HERE are the methods I tried:>>>>
[CODE]
SetGlobalInt('Money', money)
GetGlobalInt('Money', money)
[/CODE] : and when the server updates the client dosnt.
-------------------------------------------------------------------------------
[CODE]
util.AddNetworkString('MoneyTransfer')
net.Start('MoneyTransfer')
net.WriteInt(money, 3) -- or just -- net.WriteInt(money)
net.Send(ply) -- and I tried sending to server like net.SendToServer()
net.Receive('Money', function()
money = net.ReadInt(money,3) -- Or just -- net.ReadInt(money)
end) [/CODE] : This didnt work cause it didnt read it, I guess > returned nil
And yeah those are the ways and I still couldnt fix this. Please msg back soon if you can THX!
Well, if you read the wiki, you'd have noticed that SetGlobal___ just makes the value available to all clients, it doesn't grant them the ability to modify them. Any SetGlobal___ calls in client strictly modify the client's version of that value.
As for the second part, you're going to have to elaborate... Do you want a variable that is accessible to all clients, where all clients can modify it and everyone else will receive the new version? Or do you want a variable that is shared between the server and each client independently?
Shared between client and server where if server changes it then client gets that change + I dont want clients to be able to modify in game.
[CODE]
--Server
util.AddNetworkingString("example")
local your_global_var = 0
--Update the clients copy to the servers
function UpdateClients() --ply is the player to update
net.Start("example")
net.WriteInt(your_global_var , 8)
net.Broadcast()
end
net.Receive("example",function(len , ply)
your_global_var = net.ReadInt(8)
UpdateClients() -- Update all clients after receiving the new value
end)
--Client
local your_global_var = 0
--Used to tell the server about the new value
function updateServer()
net.Start("example")
net.WriteInt(your_global_var , 8)
net.SendToServer()
end
net.Receive("example" , function(len)
your_global_var = net.ReadInt(8)
end)
[/CODE]
[CODE]
--Usages
--When ever you change the value call UpdateServer or UpdateClient
your_global_var = 10
updateServer()
--and vise versa with client.[/CODE]
Give a look over what I did [url=https://github.com/AlexSwift/Ananke/blob/master/gamemode/core/shared/networking/protocols/nw_variables.lua]here[/url]. This way you can have as many variables as you want, set them as you would normally and then they automatically transmit to the client. I wouldn't advise trying to use that code, just showing you how you can make use of metatables to automate you're code quite efficiently.
[QUOTE=dingusnin;50542057]Give a look over what I did [url=https://github.com/AlexSwift/Ananke/blob/master/gamemode/core/shared/networking/protocols/nw_variables.lua]here[/url]. This way you can have as many variables as you want, set them as you would normally and then they automatically transmit to the client. I wouldn't advise trying to use that code, just showing you how you can make use of meta tables to automate you're code quite efficiently.[/QUOTE]
I agree your code is better, Or better yet for the purpose he is doing it for clients should not be allowed to changed these values anyway. I did it that way as he is very new and im pretty sure your way would confuse the hell out of him.
YOU ARE RIGHT LIKE WTF
Sorry, you need to Log In to post a reply to this thread.