Problem with modulescript

hi im trying to get a function from a module script to work in a server script, it only works locally.

heres the code:
server -

local weapons = require(game:GetService("ReplicatedStorage").WeaponsModule)

game.ReplicatedStorage.WeaponEvent.OnServerEvent:Connect(function(player, weapon)
	weapons[weapon].usage()
end)

module -

WeaponsList = {
	
	Sword = {cooldown = 5, damage = 5,
		buttontext = "sword", buttoncolor = Color3.new(0.411765, 0.294118, 0.8),
		
		usage = function()
			print("activated")
		end,
	},
	
}

return WeaponsList

local

local userinput = game:GetService("UserInputService")
local weapons = require(game:GetService("ReplicatedStorage").WeaponsModule)

weapononcd = false

userinput.InputBegan:Connect(function(input, processed)
	if input and not processed then	
		if input.KeyCode == Enum.KeyCode.Q then
			if weapons.Sword.cooldown then
				if weapononcd == false then
					weapononcd = true
					
					game.ReplicatedStorage.WeaponEvent:FireServer("Sword")
					
					task.wait(weapons.Sword.cooldown)
					weapononcd = false
				end
			end
		end
	end
end)

help (it passes the function from the modulescript into the server script where it tries to fire the function, the modulescript function should be printing on the server but it prints locally)

You can’t pass functions from the server to the client. You should pass the index for the server to look up instead.

-- Pass the index of the weapon being used
game.ReplicatedStorage.WeaponEvent:FireServer("Sword")
1 Like

Oh im not trying to pass function from server to client, I get a function from the module script table which is the code for my weapon into a local script from which i send it to the server script so it can get fired server sided if you understand me, but it activates locally and not server sided. and i want it to be server sided

I understand, however in your code example you are passing a function into :FireServer() within your local script:

-- .usage here is a function, which cannot be passed
game.ReplicatedStorage.WeaponEvent:FireServer(weapons.Sword.usage)

You should instead pass the index. Since the index is a string it can be passed to the server, where the server can use that index to look up the weapon data within the same module.

wait, ima edit that sorry for wasting your time, it was the index before, not the actual function. i was trying multiple options to fix my problem and forgot to change that before posting on forum. though the server script seems to keep printing locally. its probably firing the function inside of the module script and thats not what i want, idk why it does that

I’m quite uncertain about what you’re asking for here. I copied your code and it all appears to work just fine. The server prints “activated” and follows the cooldown. What is the issue you’re facing?

1 Like

instead of the server printing it, it prints locally somehow. Well thanks for the help tho, im gonna try and fix it myself.

it works now, probably a roblox bug then ¯_(ツ)_/¯

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.