Hello,
i have an issue with my code (obviously) anyways i need to run a function in the client from my server but i can't figure out how to. i have a rep_init.lua which i run, which has:
AddCSLuaFile( "rep_shared.lua" )
AddCSLuaFile( "rep_cl.lua" )
include("rep_cl.lua")
include( 'rep_shared.lua' )
as you can see my files are called rep_shared.lua (server side the one i want to call the function from) and rep_cl.lua (which i want to run the function on) please help if possible. i also have:
include("rep_init.lua")
inside my rep_cl.lua
btw i'm new please give me constrictive feedback.
You could try using the [URL="http://wiki.garrysmod.com/page/Net_Library_Usage"]net library[/URL]
Net Library is a HUGE...I mean MASSIVELY HUGE part of Glua. You need to understand and learn how the server and client talk back and forth. <CODE BLUE> will help you, check the link out below. But "net.Receive" is your puppy in this case.
[url]https://www.youtube.com/channel/UCFpuE-Qjn4EWqX-VJ_l7pbw/playlists[/url]
First, open the script server side. Then open it client side. The result will be in the console "1 + 5 = 6". In this case we are asking for no input, and sending with four bits.
[CODE]if (SERVER) then
util.AddNetworkString("myNetString") -- Register the Network String
function additionX(a, b) -- Simple function returns a + b
return a + b
end
net.Receive("myNetString",function() -- When the server receives the clients Network information
local a = net.ReadInt(4) -- a = 1, receiving with four bits
local b = net.ReadInt(4) -- b = 5, receiving with four bits
print(additionX(a, b)) -- Print the return value of the function, with our defined parameters.
end)
else
net.Start("myNetString") -- Start the Network String
net.WriteInt(1, 4) -- Write the first part as 1, with bits as four. The server will receive these numbers in order as we send them.
net.WriteInt(5, 4) -- Write the second part as 5, with bits as four. Realize that you have to send and receive with the same bit size.
net.SendToServer() -- Send what we have to the server.
end
[/CODE]
Or, if you want an example that could work with what you want to do:
[CODE]
-- in rep_shared.lua:
util.AddNetworkString("RunSomeFunction")
-- this would run the function in the clientside file:
net.Start( "RunSomeFunction" )
net.Send( someplayer ) -- make sure someplayer is an actual player
-- in rep_cl.lua:
net.Receive( "RunSomeFunction", YourFunction ) -- YourFunction is whatever the function you want to run is, and it should run once you run the serverside net.Start and net.Send bit
[/CODE]
Pretty simple stuff
Sorry, you need to Log In to post a reply to this thread.