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

How to call a module script as a specific client?

Asked by 3 years ago
Edited 3 years ago

I need to be able to call a module script as a chosen client from a different client. I have tried 1. Imagining script interactions that would allow me to do such a thing (to no avail) and 2. Searched the internet for people with a similar problem and for features that would allow me to fix this issue (again, to no avail).

So, does anyone know of any features or have any ideas to solve this issue? Please don't suggest alternatives as I already have some ideas, but being able to solve this problem would simplify the situation pretty significantly.

Example (not the actual code but proves the same point):

Script in StarterCharacterScripts:

local uis = game:GetService("UserInputService")
local players = game:GetService("Players")
local repStorage = game:GetService("ReplicatedStorage")
local module = require(repStorage.module)

-- Get player that isn't local player
function getOtherPlayer()
    if #players:GetChildren() > 1 then
        for i, v in pairs(players:GetChildren()) do
            if v ~= players.LocalPlayer then
                return v
            end
        end
    end
    return nil
end

uis.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.Q then
        local player = getOtherPlayer()
        if player ~= nil then
            module.test(player)
        else
            error("only 1 player!")
        end
    end
end)

Script in ReplicatedStorage:

local module = {}
local players = game:GetService("Players")

function module.test(player) 
    -- Meant to set the referenced player's (not the calling player's) 
    -- CameraSubject to their HumanoidRootPart, which does not work
    -- due to the modulescript being called from the wrong client.
    workspace.CurrentCamera.CameraSubject = 
        player.Character.HumanoidRootPart
end

return module

Sorry if my forum etiquette is bad, I'm not very good with this stuff. Please let me know if I missed anything.

EDIT (for clarification):

In response to AlexanderYar:

Sorry for being unclear, let me clarify.

The base concept is: whenever a module script is called from a script, it imitates the script's type. For example, when it is called from a server script, it runs as if it were a server script. However, when it is called from a local script, it runs on the computer of client who called it; this is the problem. I want to be able to call a module script and have it run on a specific client's computer.

In the example code, I try to change the "other player"'s camera to their humanoidrootpart, but because the module script only runs on the computer of the client who called it, it changes the calling player's camera instead of the "other player"'s camera (workspace.currentcamera is dependent on who calls it). Ideally, I would be able to call a module script from client one, and have it run on client two's computer. I don't care how it is accomplished (remote events, obscure services, etc.) as long as it works and isn't laggy.

I'm not suggesting you don't know this stuff, I'm just terrible at putting it into words. Let me know if this helped.

0
I edited my answer, hope it helps you. if not, please leave a comment. PS: I don't get notified of comments on other ppl questions/answers, so best way to contact me is to add a comment to my answer Leamir 3138 — 3y

2 answers

Log in to vote
0
Answered by
Leamir 3138 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

Hello, Hlidskjalf!


Edit: I had understood the question wrong, sorry.

What you are trying to achive can be done using RemoteEvents, to be more specific, you would need to do something like Client A -> Server -> Client B.

Explanation: Client A notifies the server that it wants to run the module as Client B, the server triggers a Client event where the recipient is Client B, then Client B will run the module normally

Useful Links:

https://developer.roblox.com/en-us/articles/Remote-Functions-and-Events

https://developer.roblox.com/en-us/api-reference/class/RemoteEvent

https://developer.roblox.com/en-us/api-reference/class/ModuleScript

https://education.roblox.com/en-us/resources/intro-to-module-scripts


Old answer:

The problem with your code is that the varible players is aways outdated as you only get the value once(at line 2 in your original code), my recommendation is to put the variable declaring inside of the function like this:

local uis = game:GetService("UserInputService")
local repStorage = game:GetService("ReplicatedStorage")
local module = require(repStorage.module)

-- Get player that isn't local player
function getOtherPlayer()
    local players = game:GetService("Players") --Putting this line here makes so the variable is reloaded every time the function is ran(updates the player list)
    if #players:GetChildren() > 1 then
        for i, v in pairs(players:GetChildren()) do
            if v ~= players.LocalPlayer then
                return v
            end
        end
    end
    return nil
end

uis.InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.Q then
        local player = getOtherPlayer()
        if player ~= nil then
            module.test(player)
        else
            error("only 1 player!")
        end
    end
end)

Useful links:

https://developer.roblox.com/en-us/articles/Variables


If this answers your question, please mark this answer as the Accepted Answer

0
Thanks for the reply, but it doesn't answer my question (my fault, sorry for bad explanation). Maybe see if my response to AlexanderYar can help you understand. Hlidskjalf -3 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

If your problem is what I think it is (its confusing to me), then can't you just get the instance of the player from the client? In getOtherPlayer() I think your trying to get a specific player thats not the localplayer, for this you can just get the instance like this

local plr = game.Players[playerName]

Then just do the modules script thing yeah?

Module.test(plr)

I hope it helps :3, if I misunderstood please tell me and I will try to help you

0
the players instances and characters are all replicated through all clients, so thats not the problem Leamir 3138 — 3y
0
I added a response, take a look Hlidskjalf -3 — 3y

Answer this question