Can someone help with InvokeServer

Hey All,
Just been granted “Member” status so this is my first post so sorry if its in the wrong place or not formatted correctly!

Anyway heres my issue:
Im trying to fetch data and have used module script for it which is onthe server however the script im trying to get the data to(?) is located in starterplayerscripts, when i run it in game it says invokeserver can only be called from the client, is there a way to still be able to use it without having to move all the scripts around as there is a few of them and i dont want to have to re-do the whole game to make it work.

this is the bit thats causing issues:

function GetPlayerData(plr)

plr = plr or Player
local UserData = FetchClientDataEvent:InvokeServer(plr)
return UserData
end

Any help would be appreciated!

Thanks

Hello!

Just a quick heads up that this should be in #help-and-feedback:scripting-support not #development-discussion

Also consider using the Lua code block so that it’s easier to read your post

function GetPlayerData(plr)
plr = plr or Player
local UserData = FetchClientDataEvent:InvokeServer(plr)
return UserData
end

Anyway, Good luck with your development!

Oh ok,thanks for the heads up, shjould i move it to there or will it be ok leaving it here? i didnt even see that, thanks again.

You should move it. Or your post could be flagged

The error you’re getting indicates that you’re calling InvokeServer() from the server, which as the error states can only be called from the client.

Are you trying to get data that is currently on the server to the client? If so, the way you’d use a Remote Function is you’d call InvokeServer() from a script on the client (your script in StarterPlayerScripts), and in a server script you’d set remotefunc.OnServerInvoke equal to a function that’ll return the data:

--CLIENT SIDE---
local UserData = FetchData:InvokeServer()

--SERVER SIDE--
function GetPlayerData(plr)
    --do something here to get the players data, then return it
    return data
end

FetchData.OnServerInvoke = GetPlayerData

Calling :InvokeServer() automatically passes the first argument as the local player calling the function.

If the reverse is true and you’re trying to get data currently stored on the client to the server, you’ll do the same but the other way round, calling InvokeClient() from the server, and hooking up a function to OnClientInvoke on the client.

Not sure if I’ve understood your issue or use case correctly, but you might also consider using a RemoveEvent instead of a RemoteFunction if you’re simply trying to send data without ‘requesting’ it.

1 Like

You don’t have to include the player as a parameter, I would also personally avoid it as it can be a security risk. By default, when firing from the client to the server, the first parameter will always be the player who fired the event.

1 Like

Is the data being returned not nil?

You should be calling Invoke Server on a Local Script, not a Server Script (Script).

Solution:

  1. Copy/cut and paste your code in a local script.
  2. Rename your local script to the name of your server script.
  3. Delete your server script.