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
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.
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.