Attempt to call nil value

I am trying to figure this out and cannot call functions from even server-side, Here is my code

server-side

function LBCore.Functions.GetPlayers()
    local peeps = 48
    local players = {}
    for i=0, peeps do
        table.insert(players, i)
    end
    return players
end

client-side

print(LBCore.Functions:GetPlayers())

You can’t call a server-side function from the client like this. Also, I think the function’s name should be LBCore.Functions.GetPlayers not LBCore.Functions:GetPlayers ?

If I can’t then why are there even server functions in the first place? I’m not arguing I’ve just been burnt out because of this

Do print(LBCore.Functions.GetPlayers())

I did, same error. I think what that other guy said was right, I have to use events. I sent you a friend request though

ah I just noticed it may have been registered improperly, are the scripts “shared” or SV-CL?

The files are SV but I have a shared script that references the names?

Okay, I would say to fix a nil value, try making a test function to see where it works and where it doesn’t,

EXAMPLE

in all files create a function called something like File1.TEST, File2.TEST, ETC

and then call them all from where you want the Getplayers to be called from

so say I want to call getplayers in test.lua

TEST.LUA:
print(FILE1.TEST)
print(FILE2.TEST)

just to figure out which files work.

Ok so I did that. I made a sh_main.lua file that is my shared, I put print(LBCore.Functions.Test()) there and it did not work. It gave me the same error. I did put the same functions in both client and server side with different prints

Based on the same logic (since you also can’t call client functions from the server) why are there even client functions in the first place? Functions, in general are good for making your code modular, improving readability, etc, doesn’t matter if it’s on the client or on the server. The client doesn’t see the address of a function defined on the server side (it sees it as nil/NULL at runtime when it is called), and that’s why you get the error.

The only way for scripts to communicate from client to server and vice versa is via events. I genuinely don’t understand why you are trying to complicate things instead of just using events. You could also just move the server code to the client?

As for shared scripts, they don’t work as you are probably thinking. Declaring a script as “shared” has the same effect as declaring it on both the client side and the server side. It will basically create 2 independent copies in memory that do not affect each other, meaning that, for example, if you modify a variable stored in the shared client script at runtime, the same shared script on the server will not be affected.

Also, your code is poorly written. What happens if you change the server’s player slots number to something greater than 48?

Hey, instead of saying everything wrong about their code, what if we decide to compliment things that they did right? The forums should be a place to help, not to tell people everything wrong such as

Also, your code is poorly written. What happens if you change the server’s player slots number to something greater than 48?

What if they didn’t know this? I understand you are asking a question but you can help them improve it, maybe give them the code to make it work?

I didn’t know if this would fix anything, but try this:

print(tostring(LBCore.Functions.GetPlayers()))

You must be joking? Congratulations on writing a for loop. And what do you mean by “not to tell people everything wrong”? I pointed out a genuine bug that is in the code he provided. If you can’t think about basic edge cases and possible errors, you shouldn’t be developing such complex framework scripts. I also will not spoon feed anything since most people don’t learn like this, they just copy paste.

Also, this is laughable:

You clearly don’t understand that you CANNOT call server side functions from the client like this. What difference could the tostring() possibly do? Please share with us the idea behind this.

xx

Ok noted. But why does the QBCore code have server functions?

QBcore is a framework. These server functions are for developers to use when writing their own scripts that integrate into the framework.

Huh, functions to use. How can we do that if you said it’s impossible?

By using exports. You export a server function and then some other script can use it on its server side code. But you cannot export a server function and call it on some other script on the client side.

I said it was impossible to call server-sided function from a client script by directly referencing it aka calling it as you were trying to do.

A little example would be:

testScript, server.lua:

local function testFunc()
	print("OK")
end

exports("testFunc", testFunc);

testScript2, server.lua:

RegisterCommand("testexport", function()
	exports.testScript:testFunc()
end)

The testScript resource is exporting a server-sided function called testFunc. Then another resource, testScript2, will call that function using exports.testScript:testFunc() from the server side when the command testexport is executed. Now if you experiment with this and move the RegisterCommand part on a client script, you will notice that you will get a No such export ... error. Server exported functions can only be seen by other server scripts. Same for client exports and client scripts.

I finally understand now/ I’ve been around QBCore’s code, not to copy, but to learn from it, and they always call their functions inside the server scripts. I’m sorry if I’ve been hard-headed lol