as you can see from the title I want to know why this codes cause memory leaks and how to solve
local ServerScriptService = game:GetService("ServerScriptService")
local Npc = script.Parent
local NPCHumanoid = Npc:FindFirstChildWhichIsA("Humanoid")
local MobModule = require(ServerScriptService.MoBs)
local NpcStandLocation = Npc:WaitForChild("HumanoidRootPart").Position
local IntValue = Instance.new("IntValue",Npc) --Creates an IntValues to Npc:(Script.Parent)
IntValue.Name = "RandomNumber"
IntValue.Value = math.random(0,100)
for i,v in pairs(MobModule) do
NPCHumanoid.MaxHealth = MobModule [Npc.Name].Hp
NPCHumanoid.Health = MobModule [Npc.Name].Hp
end
local function MoveNPC() -- Just a function to move Npc
while true do
if NpcStandLocation and NPCHumanoid and not NPCHumanoid:FindFirstChild("HumOwner") then
NPCHumanoid:MoveTo(NpcStandLocation + Vector3.new(math.random(-10,10),0,math.random(-10,10)))
else
print("One of them are wrong")
end
task.wait(7)
end
end
local co = coroutine.create(MoveNPC)
coroutine.resume(co)
after a few test I am pretty confidant Instance.New IntValue part causes it but it shouldn’t? it doesn’t have any other reference to it? it should be gced right?
Some people class using actual values such as “IntValue” or “BoolValue” - actual objects in the game as opposed to code - memory leaks.
Realistically speaking you should never really use these values unless you want to create some tweenable reference.
Especially seeming as:
local number = math.random(0,100)
Is the exact same thing, but uses less memory and isnt a publicly visible number.
It really depends what your trying to achieve - I can see reasons for your thing existing, but these reasons would also likely suggest to me that you need to change how you code things.
I was trying to give npc’s effect depends on the value they have
for example if RandomValue <10 then npc get’s red particles
and if randomValue is between 10 and 30 npc get’s blue particles
so. to track the number(or let’s say that randomValue) I couldn’t come with any better way than adding an track-able IntValue to under of npc