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

How to call module script from the server instead of client?

Asked by 1 year ago

I’ve been diving into module scripts and I know that its depended on the script that requires it whether its going to be on the server or client but I would like it to be on the server. However, you cannot use **UserInputService **on a server script so everything that happens will be on the client and I can’t figure a way around this. Could anyone help?


local GameFolder = game.ReplicatedStorage.GameFolder
local Modules = GameFolder.Modules
local PearlModule = require(Modules.PearlAbilities)
local Debounce = true

local UserInputService = game:GetService("UserInputService")
UserInputService.InputBegan:Connect(function(hit, chat)
    if chat then return end
    local ActivateAbility = PearlModule[hit.KeyCode] 
    if ActivateAbility and Debounce == true then
        local Cooldown = ActivateAbility.Cooldown
        Debounce = false
        Debounce = true


local TweenService = game:GetService("TweenService")
local Players = game.Players
local Cooldown = nil
local Abilitys = {
    [Enum.KeyCode.Q] = {

        Activate = function(Player)
            local Character = Players.LocalPlayer.Character
            local Gem = Character:WaitForChild("Gem")

            if Gem.Light.Brightness == 0 then
                Gem.Material = Enum.Material.Neon
                TweenService:Create(Gem.Light,, {Brightness = 10} ):Play()

                Gem.Material = Enum.Material.SmoothPlastic
                TweenService:Create(Gem.Light,, {Brightness = 0} ):Play()
        Cooldown =  1.1
    [Enum.KeyCode.F] = {
        Activate = function(Player)
            local Character = Players.LocalPlayer.Character
            local Humanoid = Character:WaitForChild("Humanoid")
            local Health = Humanoid.Health
            local MaxHealth = Humanoid.MaxHealth
            local timer = 15

        Cooldown =  5


return Abilitys

1 answer

Log in to vote
Answered by 1 year ago

Well, I'm not sure why you would need to require that module on the server. As you said, the module is dealing with user input so it has no business being required server-side.

However, there are cases where modules may have both server-side and client-side exclusive applications, and when that is the case, you can always check to see which machine your code is running on by using RunService:IsServer() and RunService:IsClient().

Therefore, you could modify your module code to only access the UserInputService if the script is running client-side. For example:

local RunService = game:GetService('RunService')

local myModule = {}

if RunService:IsClient() then
    local UserInputService = game:GetService('UserInputService')
    -- ... rest of client-exlusive code here
    -- ... rest of server-exclusive code here

return myModule

Let me know if you have any questions.

The reason why I want to be required on the server is because I want others to see when you use your abilities Funnyskyswagway3 30 — 1y
In that case you should trigger a remote event telling the server that some ability was used, then have the server fire all clients so they can render the effects locally. ScriptGuider 5640 — 1y

Answer this question