Using a function defined in init.lua in cl_init.lua

I’m trying to make a door own/lock/unlock addon similar to the one on DarkRP. I have a function in my swep on init.lua as shown here


function getDoorOwner()

	return "sample text"

end

I’m trying to print it to the console so i can later cam.Start3d2d it and show it on doors.
However i get this error when trying to use that function client side.


[ERROR] gamemodes/x/entities/weapons/keys/cl_init.lua:21: attempt to call gl
obal 'getDoorOwner' (a nil value)
  1. unknown - gamemodes/x/entities/weapons/keys/cl_init.lua:21

Does this happen because the function is defined serverside?

Yes.

This function will be accessible if i place it in the shared file, but players will be able to somehow change the values with clientside lua, correct?

You’re best just setting the owner through a NWString, and then retrieving it clientside.

Should i set the NWString in the serverside file or the shared?

Youd set the NWvar serverside. You can do it in an initialize hook like this:
[lua]

local function initDoorOwner()

for k, v in pairs( player.GetAll() ) do

local plyName = tostring(  v:Name() )
v:SetNWString( "doorOwner", plyName )

end

end
hook.Add(“Initialize”, “initDoorOwner”, initDoorOwner)

[/lua]

im doing this on a phone, so the formatting is probably terrible.
Anyways, from here you can then later set the string or store it by using:

[lua]

ply:SetNWString( “doorOwner”, “whateveryouwant” ) – Sets the string

local getDoorOwner = ply:GetNWString( “doorOwner” ) – Just store it for later use.

[/lua]

Good thing about these is you can use them from any script just by use GetNWString or whatever the type is once theyre initialized.

you can whenever you want do



ent:SetNWString( "something", something );


and from any client do



print( ent:GetNWString( "something", "nothing" ) );


the point with these variables are that they are networked automatically, so when a player joins they know this networked string “something” on ent, and once the value changes, it’s updated for them.

I don’t know what angrypygmy tried to do by looping through players, what he does is foreach player, set a networked variable on the player with the identifier “doorOwner” containing the players name.

shared.lua


function ENT:SetupDataTables()

	self:NetworkVar( "Entity", 1, "owning_ent" ) -- Type, slot, name

end

cl_init.lua



    local owner = self:Getowning_ent()

    owner = (IsValid(owner) and owner:Nick())


Not 100% if it’s what you were looking for, lol.