Hello! So I’m planning on making a game and there will be abilities, like when you click mouse, click E and stuff. How would I go about this? A local script that handles all the keys clicked and a module script for all the abilities? I need some advice so this would turn out efficient and easy to customize.
so you would choose your ability and press a certain hotkey to activate it, or just each ability has its own hotkey to activate it?
I would recommend using ContextActionService (1) in a local script to detect the button the user is pressing. Use a RemoteEvent (2) and fire the RemoteEvent from the said local script, make sure you have a server script stored somewhere on the server side where you can make the actual ability script and from there you can begin on making an abilities system.
(1) https://developer.roblox.com/api-reference/class/ContextActionService
(2) https://wiki.roblox.com/index.php?title=Remote_Functions_%26_Events
Each ability has a different key.
well I guess since you’re making each ability to a different key, make a dictionary and set the index of each element as a keycode and the value of it would be the function (ability) in a module script from the server. Then you could listen for the InputBegan
event of UserInputService and fire to the server in a RemoteEvent with the KeyCode as the argument and loop or iterate through the table to check if the KeyCode is equal to the KeyCode (index) of the ability. So I guess something like this-
server:
local event = game.ReplicatedStorage.RemoteEvent
local abilities = require(script.ModuleScript)
local abilityTable = {[Enum.KeyCode.E] = abilities.HealthBoost}
event.OnServerEvent:Connect(function(player, keycode)
for key,ability in pairs(abilityTable) do
if key == keycode then
ability(player)
break
end
end
end)
modulescript:
local module = {}
function module.HealthBoost(player)
print("test", player.Name)
end
return module
client:
local uis = game:GetService("UserInputService")
local event = game:GetService("ReplicatedStorage").RemoteEvent
uis.InputBegan:Connect(function(input)
if input.UserInputType = Enum.UserInputType.Keyboard then
event:FireServer(input.KeyCode)
end
end)