How to execute more than one piece of code inside a module script at once?

. I’m creating a game that requires storing cooldowns, damage, defence(armour), debuffs, buffs and any other values that need to be dynamic. This means these values will need to be alterable at any time. One attack from a player might decrease the damage of the target hit by the attack, add a debuff and lower their defence.

I’ve realised that I can only execute code inside a module script once at a time, I was wondering if there’s a way I can do this that isn’t hacky.

Example of the specific issue

I cloned this twice. Cloning this twice I would expect the module script’s function to fire twice as when I press F it’ll fire the local script code twice, it doesn’t do this.

Local Script

– This sends a remote event to the server script:

local UserInputService = game:GetService("UserInputService")
local RemoteEvent = game:GetService("ReplicatedStorage").TestEvent

local function OnInputBegan(input, gameProcessedEvent)
	if input.KeyCode == Enum.KeyCode.F then 
		print("Text says", script.Parent.Text)
		if game.Players:FindFirstChild(script.Parent.Text) then 
RemoteEvent:FireServer(script.Parent.Text)
		end
	end
end


UserInputService.InputBegan:connect(function(input, gameProcessedEvent)
	OnInputBegan(input, gameProcessedEvent)
end)

What this is doing is there’s a textbox which this is a child of, this is for testing purposes. I will enter the players name and it’ll search for the name under the serverstorage folder named “AllStats” because as I mentioned previously, a module script was cloned on join which is named after the player.

Server Script

–This calls the function located inside the module script, it gets the text from the text box which I got in the local script above and then applies any damage to the player who’s name matches the textboxes text.

local RemoteEvent = game:GetService("ReplicatedStorage").TestEvent
local req = nil


local function ChangeValue(player, Text)
	req = require(game.ServerStorage.AllStats:FindFirstChild(Text))
	req.Damage(Text, 10, 0, nil)
end


RemoteEvent.OnServerEvent:Connect(ChangeValue)

This will call the function located inside ServerStorage.AllStats(Text, this tracks all the way back to local script where I passed the variable of the textbox I made for testing purposes)

Module Script

– This is the template for the module script. It is cloned on join and named after the player who joined, it then goes into ServerStorage.AllStats.

local PlayerValue = {}

PlayerValue.Debuffs = {}
PlayerValue.Buffs = {}
PlayerValue.Stats = {Damage = 10, Defence = 10}

PlayerValue.Debuffs = {"Test"}  
local player = script.Name

function PlayerValue.Damage(Target, Damage, Modifier, ReceivedFrom)
	
	local Char = game.Workspace:FindFirstChild(player)
	local Health = Char.Humanoid.Health	
	local Target = game.Workspace:FindFirstChild(Target)
	local Humanoid = Target:FindFirstChild("Humanoid")
	
	if PlayerValue.Stats.Defence > 0 then 
		
		print("-- Beginning of script--")
		print("Damage:", tostring(Damage))
		print("Defence:", tostring(PlayerValue.Stats.Defence))
		print("-- End of script --")
		PlayerValue.Stats.Defence -= Damage

		
	else if PlayerValue.Stats.Defence <= 0 then
			
		Health -= Damage
		
	end
end
end

return PlayerValue

This is a generic module script that will lower the player’s defence value. The amount of damage dealt was defined when I called the function inside of the server script. I get the player by the module script’s name because the module script is named after the player.

If you want to execute multiple functions at the same time then you’ll need to make use of multiple threads, you can do this through either using the built-in function named “spawn()” or through the coroutine library.

1 Like

Thank you! I’ll look into these, edited from my previous which was a mistake. This should function well with the way I’ve set it up.

Note, now that task.spawn is a thing, you should prefer it over the legacy global spawn.