Module script to load skills from a specific weapon

Ok so, I want to make something that stores a specific weapon, like a Sword, and has different skill for that specific weapon. I heard that module scripts were a good way to do this all. In general I want a weapon ability system similar to blox fruits.

I think this image is a good example of what i want to do like i want to grab the values from this table in a module and then fire a certain ability

2 Likes

What works best comes up to personal preference, but I can show you how I would setup something like this.

local  Weapons = {
	MagicSword = {
		Name = "Magic Sword",
		Ability1 = function()
			--code
		end,
	},
	FireHammer = {
		Name = "Fire Hammer",
		Ability1 = function()
			--Code
		end,
	}
}

return Weapons

This way you can reference the weapon in code like this

local Weapons = require(game.ReplicatedStorage.WeaponsModule)

print(Weapons.MagicSword.Name)
Weapons.MagicSword.Ability1()

You can also go further if you want the ability to have a name:

local  Weapons = {
	MagicSword = {
		Name = "Magic Sword",
		Ability1 = {
			Name = "Magic Ability",
			Activate = function()
				--Code
			end
		}
	}
}

return Weapons

Which can be referenced like this

local Weapons = require(game.ReplicatedStorage.WeaponsModule)

print(Weapons.MagicSword.Name)
print(Weapons.MagicSword.Ability1.Name)
Weapons.MagicSword.Ability1.Activate()
2 Likes

Repost since you made 4 duplicate topics, the other 3 are now removed.

Good that you’re doing this with ModuleScripts!

This is how I would recommend to do it:

local Earth = {
    ["Rock Grasp"] = function()
        print("First ability")
    end,
    ["Multi-Rock Throw"] = function()
        print("Second ability")
    end
}

return Earth

Then, in another script:

local Earth = require(script.Earth)
Earth["Rock Grasp"]()

This will run the code in your ModuleScript and print “First ability” in the Output window.


This data structure is good to work with if order doesn’t matter. You can refer to the function by using the name in your code.

→ This means: if you want to enforce a specific order or based on keybind, you should use that as index instead of the name. Can elaborate if needed.

1 Like

question, should I reference the module script in a local script? I’m assuming that I’m supposed to grab values from the module script inside the local script and then fire a remote event to the server

could you elaborate on that? I dont get what you mean by use as index instead of the name. I want to use keybinds to do skills

You can reference stuff such as names in local scripts, if you want to display the ability names to the player as an example, but activate the abilities on the server, by yes, use of a remote event.

To clarify, use the ModuleScript on the server.

You want to pass the action name from the client to the server, like:

game.ReplicatedStorage.SomeEvent:FireServer("Rock Grasp")

When you have keybinds, put that on the client, find the name and send that name to the server.

Practically, on the client:

local keybinds = {
    [Enum.KeyCode.E] = "Rock Grasp"
}

game.ReplicatedStorage.SomeEvent:FireServer(keybinds[Enum.KeyCode.E])

wouldnt it be more clean if i grabbed the keybinds table from the module script and then put that in the local script?

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