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 7 years ago
Edited 7 years ago

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

function move(x, y, z)
    --body
end

function eat(food)
    --body
end

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

function move(x, y, z)
    -- body
end

function eat(food)
    -- body
end
game.ReplicatedStorage.Event.OnClientEvent:connect(function(request, func, ...)
    if request == "runFunction" then
        if func == "move" then
            args = {...}
            x = args[1]
            y = args[2]
            z = args[3]
            move(x, y, z)
        elseif func == "eat" then
            args = {...}
            food = args[1]
            eat(food)
        end
    end
end)

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:

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

game.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 — 7y
Ad

Answer this question