I’m making a game where players can equip perks that run passively, without player input. Each perk has a ModuleScript that handles its code.
However, some of those perks should only activate when a function from the Player Script is called.
For example, a perk that temporarily highlights the player and the aggressor every time the player takes damage.
How can I connect the highlight function from the ModuleScript with the function from the Player Script that handles health and damage, so they both run at the same time?
Access (require?) a module from one script to another or fire a single event that they’re both connected to (or fire an event with one script that the other is connected to)
-- Module script
local perks = {
['aggressor'] = function()
return 'whatever your code is'
end
-- Add other funcs too
}
return perks
-- Call the script somewhere else
local module = require(game.ReplicatedStorage.ModuleScript)
local f = module.aggressor()
print(f) -- 'whatever your code is'
id recommend using the “creator” tag that most old combat systems (and maybe new, idk) use.
then you could try something like this
-- module
function module.highlight()
local creator = humanoid:FindFirstChild("creator")
if (creator and creator:IsA("ObjectValue") and creator.Value and creator.Value ~= nil) then
for i, v in pairs({creator.Value.Character,humanoid.Parent}) do
local highlight = Instance.new("Highlight",v)
game:GetService("Debris"):AddItem(highlight,4)
end
end
end
-- other script
humanoid.HealthChanged:Connect(module.highlight)