Script Activity slowly rising


My script increases in Activity gradually overtime (took 1 minute to rise to 23%)
The script is mainly a while loop (with a few functions inside the loop)

Could this be due to connections/listeners (e.g. script.Parent.Touched) that I didn’t disconnect properly or is it something else

2 Likes

There is no real way to say for certain without actually seeing the code.

1 Like

Here is the code


task.wait(1)
local ms = game:GetService("MarketplaceService")

local mods = game.ReplicatedStorage:WaitForChild("Modules")
local data = mods:WaitForChild("Data")
local buildingData = require(data:WaitForChild("BuildingData"))
local oreData = require(data:WaitForChild("OreData"))

local owner = script.Parent.Parent.Name --Should be a folder named the player's name under game.Workspace.Builds
local oresCollection = game.Workspace.Ores:FindFirstChild(owner)

local events =game.ReplicatedStorage:WaitForChild("Event")
local playEffect = events:WaitForChild("PlayEffect")

local params = OverlapParams.new()
params.FilterDescendantsInstances = {oresCollection}
params.FilterType = Enum.RaycastFilterType.Include
params.RespectCanCollide = true

local selfStats = buildingData.getStat(script.Parent.Name)
local range = selfStats.Range
local walkSpeed = selfStats.WalkSpeed
local percentageDamage = selfStats.DamagePercentage
local cooldown = selfStats.Cooldown
local capacity = selfStats.Capacity

local currentMinerPos = script.Parent.Hitbox.Position

--[[

local function setupEvent(plr)
	playEffect:FireClient("SimulateMiner", {outpost = script.Parent,char = script.Parent.Miner, stats = selfStats})
end

playEffect:FireAllClients("SimulateMiner", {outpost = script.Parent,char = script.Parent.Miner, stats = selfStats})
game.Players.PlayerAdded:Connect(setupEvent)
]]

local pc1, c2 
pc1 = script.Parent.Hitbox.ProximityPrompt.Triggered:Connect(function(plr)
	if plr.Name == owner then
		local money = script.Parent.Data.SavedStats.Money.Value
		script.Parent.Data.SavedStats.Money.Value = 0
		plr.leaderstats.Money.Value += money
		playEffect:FireClient(plr , "MoneyEffect" , {money = money, pos = script.Parent.HeadLevel.Position, size = 2})
		playEffect:FireClient(plr , "MoneySound",{})
	else --Not owner
		local hs = plr:WaitForChild("hiddenstats")
		local target = hs:WaitForChild("MachineTarget")
		target.Value = script.Parent
		
		ms:PromptProductPurchase(plr , 3306311890)
	end
end)
c2 = script.Parent.Destroying:Connect(function()
	pc1:Disconnect()
	c2:Disconnect()
end)

if oresCollection then
	
	local targetOre
	local current
	local currentFunctionCode
	local function getRandomTarget(ores,parse)
		if parse >= 5 then
			return nil
		end
		if #ores > 0 then
			local target = ores[math.random(1,#ores)]
			if target:IsDescendantOf(oresCollection) and target.Parent:IsA("Model") and target.Parent.PrimaryPart == target then
				return target
			else
				return getRandomTarget(ores, parse +1)
			end
		else
			return nil
		end
		
	end
	local function getNewOre()
		local functCode = math.random(1,1000000)
		currentFunctionCode = functCode
		
		--print("New Get Ore")
		--Find ore in range
		local ores = workspace:GetPartBoundsInRadius(script.Parent.Hitbox.Position,range,params)
		targetOre = getRandomTarget(ores,1)
		
		if targetOre then
			
			script.Parent.Data.Stats.Target.Value = targetOre.Parent
			local currentTarget = targetOre
			
			local c
			c = currentTarget.Parent.Destroying:Connect(function()
				c:Disconnect()
				getNewOre()
			end)
			
			if currentFunctionCode == functCode then
				local processEnded = false
				--print((cooldown/percentageDamage) + 5)
				task.delay((cooldown/percentageDamage) + 5, function()
					if not processEnded then
						--print("Interrupt: Mining Timeout")
						getNewOre()

					end
				end)
				
				
				local dur = currentTarget.Durability.Value
				local maxDur = currentTarget.MaxDurability.Value
				
				local miningInterrupted = false
				for i = 1, math.ceil(dur/math.max(math.round(percentageDamage*maxDur),1)) do
					if not (currentTarget and currentTarget.Parent) then miningInterrupted = true break end
					if currentTarget ~= targetOre then miningInterrupted = true break end--print("Target ore misalign with current Target") 
					if currentFunctionCode ~= functCode then miningInterrupted = true  break end--print("New function detected")
					--print("Mining")
					currentTarget.Durability.Value += -math.max(math.round(currentTarget.MaxDurability.Value * (percentageDamage)),1)
					task.wait(cooldown)
				end
				if not miningInterrupted and currentTarget.Parent and currentTarget.Durability.Value <= 0 then
					--warn("Broke Ore")
					c:Disconnect()
					local oreName = currentTarget.Parent.Name
					currentTarget.Parent:Destroy()
					currentMinerPos = currentTarget.Position
					script.Parent.Data.Stats.CurrentPos.Value = currentMinerPos
					local oreStats = oreData.ores[oreName]
					if oreStats then
						script.Parent.Data.SavedStats.Money.Value += math.clamp(math.floor(oreStats.Reward* script.Parent.Data.Stats.MutationMult.Value),0, capacity)
						processEnded = true
						getNewOre()
					end
				end
			end
		else
			targetOre = nil
			script.Parent.Data.Stats.Target.Value = nil
			task.wait(2)
			getNewOre()
		end
	end
getNewOre()

end