Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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?

LocalScript:

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
		ActivateAbility.Activate()
		wait(Cooldown)
		Debounce = true
	end
end)

ModuleScript:

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,TweenInfo.new(1), {Brightness = 10} ):Play()

			else
				Gem.Material = Enum.Material.SmoothPlastic
				TweenService:Create(Gem.Light,TweenInfo.new(0.5), {Brightness = 0} ):Play()
			end
		end,
		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
			
			print(Character.Name)
		end,
		Cooldown =  5

	}
}


return Abilitys

1 answer

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
else
	-- ... rest of server-exclusive code here
end

return myModule

Let me know if you have any questions.

2 comments

0

The reason why I want to be required on the server is because I want others to see when you use your abilities

0

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.