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

I changed it to key and get the same same error

My bad sorry

(I don’t have access to studio right now so I couldn’t confirm my theory)

I see, in your remote event you didn’t send the input through so basically it is saying playerDebounces[player][nil] take this as a grain of salt I am testing the code rn. Just a theory

1 Like

So I’m actually wondering, is the script getting the correct KeyCode to fire the function in the module script? I tried using print but it wont get pass this line

If the error is before that line then it obviously won’t get past

That’s because I removed this line for now just to test something

1 Like

Try printing the variable input, what do you get?

18:10:27.214 nil - Server - AbilityCall:15

Change these following lines to:

if (input == nil and typeof(input) ~= "EnumItem") or (playerDebounces[player][input]) then return end

As for the local script, pass the keycode instead:

evnt:FireServer(hit.KeyCode)
1 Like

Server

local event = game:GetService("ReplicatedStorage").GameFolder.Events.Server
local PearlModule = require(script.PearlAbilities)
local playerDebounces = {}

event.OnServerEvent:Connect(function(player, input)
	-- Creates a table for that player if it doesn't exist
	if playerDebounces[player] == nil then
		playerDebounces[player] = {}
	end

	-- Checks if input is not an Enum and if there is debounce
	if (input == nil and typeof(input) ~= "EnumItem") or (playerDebounces[player][input]) then return end

	-- make a debounce for that specific keycode
	playerDebounces[player][input] = true

	-- If it finds that ability then fire it and wait until setting debounce to nil
	local ability = PearlModule[input]
	if ability then
		-- Fire the function on a pcall just incase it errors
		pcall(function()
			ability.Activate(player) -- This will be server-sided
		end)
		task.wait(ability.Cooldown)
	end

	-- Gets rid of the debounce set on that key
	playerDebounces[player][input] = nil
end)

Client

local event = game:GetService("ReplicatedStorage").RemoteEvent

UIS.InputBegan:Connect(function(hit, chat)
	if chat then return end
	
	event:FireServer(hit.KeyCode)
end)

Ok so now its printing the actual key but its not firing the function in the module script when you press the correct key

1 Like

Try:

if ability.Activate then
...

18:19:30.009 ServerScriptService.AbilityCall:19: attempt to index nil with ‘Activate’ - Server - AbilityCall:19

change it back to if ability then. I’m not sure because it worked for me.

It’s weird because the debounce even works but i’ts not actualy firing the function

Module

local Abilitys = {
[Enum.KeyCode.Q] = {

  Activate = function(Player)
  	local Character = Players.LocalPlayer.Character
  	local Gem = Character.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
  },

You are still trying to reference LocalPlayer. Remember that this is server side now. Use the player argument:

Activate = function(player)
	local Character = player.Character
	...
end

Thank you for the help @TheManInCity and @incognitobot_rblx :slight_smile:

2 Likes