Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would i call a function from a client script to a server script?

Asked by 6 years ago

I need a function in the Server Script that will receive an Int, and return a boolean value back to the Local Scripts. I am told i need a Function Event or whatever, but no one can explain it simple enough. Pretty much, i wanna use a function across server scripts to some local scripts, but please dont over complicate thing. (btw, this server script is basically used to check if the player has a gamepass. So, nothing really complicated)

0
RemoteFunctions and RemoteEvents. The wiki explains it all. http://wiki.roblox.com/index.php?title=Remote_Events_and_Functions UgOsMiLy 1074 — 6y
0
It doesnt explain it simply. Thats the problem User#19492 0 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Ok, so I'm going to try to kindof explain you how to use Remotes. There are 2 remotes, the Remote Function and the Remote Event.

Remote Function: Script A invokes the Remote Function. Script B listens to that remote and executes a code, while script A yields(waits) until script B finishes. If you want, script B can return a value to script A.

Remote Event: Script A fires an event to the Remote Function. On script B, it listen to the event with :Connect and executes a code, while script A continues executing it's code, and doesn't wait until script B finishes executing. Script B cannot return a value to script A.

Remotes are a mean of communication between the server and the client. A server script can invoke/fire a remote and a local script listens to that and executes code, and vice-versa. But you can't use remotes to communicate server to server, client to another client, or client to same client.

------[INVOKING REMOTE FUNCTIONS]------

Local Script -> Server Script

LOCAL SCRIPT:

local remoteFunction = locationOfRemoteFunction

local remFuncReturn = locationOfRemoteFunction:InvokeServer(param)

SERVER SCRIPT:

local remoteFunction = locationOfRemoteFunction

remoteFunction.OnServerInvoke = function(plr, params)
    --Execute code

    return "potato"
end

Server Script -> Local Script

SERVER SCRIPT:

local remoteFunction = locationOfRemoteFunction
local plrToInvoke = plrLocation

local remFuncReturn = remoteFunction:InvokeClient(plr, param)

LOCAL SCRIPT:

local remoteFunction = locationOfRemoteFunction

remoteFunction.OnClientInvoke = function(param)
    --Execute code

    return "potato"
end

------[FIRING REMOTE EVENTS]------

Local Script -> Server Script

LOCAL SCRIPT:

local remoteEvent = locationOfRemoteEvent 

remoteEvent:FireServer(params)

SERVER SCRIPT:

local remoteEvent = locationOfRemoteEvent 

remoteEvent.OnServerEvent:Connect(function(plr, params)
    --Execute code
end)

Server Script -> Local Script

SERVER SCRIPT:

local remoteEvent = locationOfRemoteEvent 
local plrToFire = plrLocation

remoteEvent:FireClient(plr, param)

LOCAL SCRIPT:

local remoteEvent = locationOfRemoteEvent 

remoteEvent.OnClientEvent:Connect(function(param)
    --Execute code
end)

Notice that the first parameter of the server remotes when a local script calls a server script is a player parameter. That parameter is filled automatically with the player that called that function. And when the server calls a local script, you need to give a specific player you want to call.

You want to place the remotes on ReplicatedStorage. It's probably the best place to store remotes.

Now with the example above, you want the server to receive an int and return a bool to the client. As you want to return something, we need to use a RemoteFunction.

SERVER SCRIPT

local remoteFunction = locationOfRemoteFunction

remoteFunction.OnServerInvoke = function(plr, intParam)
    --Execute code

    return boolean
end

LOCAL SCRIPT

local remoteFunction = locationOfRemoteFunction

local serverBoolean = remoteFunction:InvokeServer(intParam)
0
Nicely done. Florian27 76 — 6y
Ad

Answer this question