Can't return string from InvokeServer

I’m working on my new shop system, and I want to return the results of the transactions, for example:
Player request a sword to buy
Server checks if the player already bought the sword
Return result: “Player already bought the sword”

The problem is that I’m getting an error (Server and client):

Attempt to concate nil with String

The server script example:

local function x(Player, Argument)
    local Item = Shop[Argument]
    if Item then
        if Player.Objects[Item.Name] then
            return "Object is real"
        else
            return "You don't have the object"
        end
    end
end

ServerEvent.OnServerInvoke = x

Currently, I can’t give the script because I’m on Mobile right now, if you need more information to help me, you can ask me then.

I don’t know why this is happening, I didn’t find any article in the developer hub.

I hope you help me, thanks for reading.

I would assume Item is non-existent, which then the function would return nil.

To confirm this is the case, add a random return statement outside of your condition scope-

local function x(Player, Argument)
    local Item = Shop[Argument]
    if Item then
        if Player.Objects[Item.Name] then
            return "Object is real"
        else
            return "You don't have the object"
        end
    end
    return "Item is not existent"
end

I don’t think, otherwise, there would be a different error in the output, I want to say, that I’m returning a message to the client.

You only posted the server code, so it’s hard to know exactly what’s going on.

Based on the error, you’re attempting to combine a string with nil, and I’m assuming your function here isn’t returning anything.

Try it and see.

Client script is basically a code requesting to buy something and waiting for a response (returning something), and change a text with the response. I dont think there is more stuff to explain, but I will try to get the code.

What do you mean when I’m attempting to combine a string with nil?

Edit: here i got the server script:

You’re setting AddItem.Name = ItemID but later on you’re using ItemID as a dictionary.
which leads me to believe ItemID is a string and not a dictionary giving you the error.

-- ItemID is not a dictionary but appears to be a string
return "I've bought "..ItemID.."!"

OR

If your Item declaration contains a “Name” property you could use that instead

return "I've bought "..Item["Name"].."!"

ItemID is a table containing a bunch of data, an example would be it’s name. In fact, ItemID[“Name”] is a string.

But in your code you would be setting AddItem.Name equal to a table which wouldn’t work

What does this print in the output?

Looks like the issue is in the line:

return "I've bought "..Item["Name"].."!"
Since its a concatenation error string with nil; I’m guessing Item[‘Name’] is non-existant, so it has no value defined under the key ‘Name’.

Be aware of limitations that the remote functions can handle, it looks like the player is providing a table. And mixed tables generally don’t work with remote functions. Check here for details on that:

1 Like

Right, I forgot that Item is the table and not ItemID, I’m sorry. Thanks!

1 Like

We all make mistakes it’s alright, I hope you got your script working!

1 Like