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

How to add class to a player and call from local scripts?

Asked by 8 years ago
Edited 8 years ago

I need to create a class or group of functions like:

1function move(x, y, z)
2    --body
3end
4 
5function eat(food)
6    --body
7end

and add this in the caracter (or what ever, I need this in the client), and call this functions from the client, but this has to be separate files.

Any help will be appreciated.

1 answer

Log in to vote
2
Answered by 8 years ago

Well, what I'm getting at is that you need a module script or a RemoteEvent. I highly recommend a Remote Event.

Remove Event and Remote Function Tutorial

The difference between a Remote Event and Remote Function is that a Remote Function has a callback so you can return a value.

First up, you need to create a RemoteEvent. You should always put RemoteEvents and RemoteFunctions inside of ReplicatedStorage. I created one and called it 'Event'

Now, from what I'm getting at you want the functions inside of the Client and the functions to be called by the Server. Here's the Client script:

01function move(x, y, z)
02    -- body
03end
04 
05function eat(food)
06    -- body
07end
08game.ReplicatedStorage.Event.OnClientEvent:connect(function(request, func, ...)
09    if request == "runFunction" then
10        if func == "move" then
11            args = {...}
12            x = args[1]
13            y = args[2]
14            z = args[3]
15            move(x, y, z)
View all 22 lines...

When you use ... as an argument, it basically creates let's you pass infinite arguments after. If you do args = {...} that turns all the arguments passed into a table. In your server script:

1game.ReplicatedStorage.Event:FireClient("runFunction", "move", 5, 8, 10) -- Runs the function 'move' with x = 5, y = 8, and z = 10

or you can do this for eat:

1game.ReplicatedStorage.Event:FireClient("runFunction", "eat", "apple") -- Runs the function 'eat' and passes 'apple'

Hope I helped! If you want to learn about ModuleScripts be sure to check this out:

Module Script Video Tutorial

If I did help out, please let me know :)

0
Help a lot, thanks, I all ready implement the remote function LordSalchipapas 96 — 8y
Ad

Answer this question