I’m trying to design a killstreak system where it multiplies your damage by a small amount after each kill, but right now I’m getting an “attempt to perform arithmetic (mul) on number and nil” error. I know that there have been previous posts about this, but I’m not sure if it applies to my case. I have placed the damage multiplier value on a module script:
local DmgIncrease = game.ReplicatedStorage.DmgIncrease
local dmgMultiplier = {}
DmgIncrease.Event:Connect(function()
dmgMultiplier.Value = 10
end)
return dmgMultiplier
Then I’m attempting to put the value into another script which has a damage value in it:
local multiplier = require(game.ServerScriptService.Misc.Killstreaks)
local dmg = 10 * multiplier.Value
when your module script is required it is run from top to bottom. The problem is connected functions do not run until the event is fired. dmgMultiplier.Value = 10 is not run until much later, you need to add a default value to dmgMultiplier.Value.
local DmgIncrease = game.ReplicatedStorage.DmgIncrease
local dmgMultiplier = {
Value = 1
}
DmgIncrease.Event:Connect(function()
dmgMultiplier.Value = 10
end)
return dmgMultiplier
Another problem you will run into is that local dmg stores the computed value (10) at the start of the script. Later when you use dmg it will always be 10 even if multiplier.Value changes. You need to recompute every time you use dmg, or at least when you know it could’ve changed.
local multiplier = require(game.ServerScriptService.Misc.Killstreaks)
local dmg = 10 * multiplier.Value
print(dmg) -- 10
multiplier.Value = 1234
print(dmg) -- 10 needs to be recalculated!
dmg = 10 * multiplier.Value
print(dmg) -- 12340