Hello peoples.
Right now I’m trying to make a script that buff nearby NPC stats. And the issue is that It just keep decreasing the npc stats and wont stop.
Is there a way i could get the old value so it wont keep decreasing?.
local stats = require(script.Parent.Stats)
local tower = script.Parent
local range = stats.Range
while task.wait(1) do
if script.Parent.Parent == game.Workspace.Towers then
for i,v in pairs(workspace.Towers:GetChildren()) do
local TowerStats = require(v.Stats)
local Distance = (tower.HumanoidRootPart.Position - v.HumanoidRootPart.Position).Magnitude
local towerInDistanceStats = require(v.Stats)
local towerFirerate = towerInDistanceStats.Cooldown
if Distance <= range then
if TowerStats.Cooldown == TowerStats.Cooldown then
local newFireRate = towerFirerate * 0.4
TowerStats.Cooldown = newFireRate
print(TowerStats.Cooldown)
else
return
end
end
end
end
end```
Keep track of the old value in a variable. Idk how that will help with the decreasing issue, but that’s how you’d get the old value.
In the while
loop, you are repeatedly decreasing the values without considering the initial value; all you need to do is save the original NPC stats before updating them.
local stats = require(script.Parent.Stats)
local tower = script.Parent
local range = stats.Range
local statsTable = {}
local function statsDecrease(npcStats)
if npcStats.Cooldown <= npcStats.OriginalCooldown * 0.4 then
npcStats.Cooldown = npcStats.OriginalCooldown
else
npcStats.Cooldown = npcStats.Cooldown * 0.9
end
end
while task.wait(1) do
if script.Parent.Parent == game.Workspace.Towers then
for _, npc in pairs(workspace.Towers:GetChildren()) do
local npcStats = require(npc.Stats)
local distance = (tower.HumanoidRootPart.Position - npc.HumanoidRootPart.Position).Magnitude
if distance <= range then
if not statsTable[npc] then
statsTable[npc] = {
Cooldown = npcStats.Cooldown,
}
end
local originalCooldown = statsTable[npc].Cooldown
decreaseStats(npcStats)
print(npcStats.Cooldown)
else
if statsTable[npc] then
npcStats.Cooldown = originalCooldown
statsTable[npc] = nil
end
end
end
end
end
When an NPC gets within range, the original NPC stats are kept in a table called statsTable
in this updated script. Also implemented new function statsDecrease
which is in charge of lowering the stats based on the reasoning you specify, its mostly just for organizing the script.
This might work but, I dont think i should add an OriginalCooldown values inside the module script because there’s alot of them.