Hello, I am making a parkour game but I've encountered a memory leak wich is coming from my 3 scripts. I noticed this script is creating a memory leak since the games lag disapeared when i disaled the script. I have already combined them into one but it still creates lag. Could someone look into this and help me?
--Vault System
local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local HRP = char:WaitForChild("HumanoidRootPart")
local Hum = char:WaitForChild("Humanoid")
local CA = Hum:LoadAnimation(script:WaitForChild("ClimbAnim"))
local ledgeavail = true
topspeed = 20
char.Humanoid.Running:Connect(function(speed)
if speed > topspeed then
ledgeavail = true
elseif speed < topspeed then
ledgeavail = false
end
while game:GetService("RunService").RenderStepped:Wait() do
r = Ray.new(HRP.Position, HRP.CFrame.LookVector * 6.5 + HRP.CFrame.UpVector * -5)
local part = workspace:FindPartOnRay(r,char)
if part and ledgeavail then
if part.Name == "Vault" then
if Hum.FloorMaterial ~= Enum.Material.Air then
if speed < topspeed then
ledgeavail = false
local Vel = Instance.new("BodyVelocity")
Vel.Parent = HRP
Vel.Velocity = Vector3.new(0,0,0)
Vel.MaxForce = Vector3.new(1,1,1) * math.huge
Vel.Velocity = HRP.CFrame.LookVector * 60 + Vector3.new(0,28,0)
CA:Play()
game.Debris:AddItem(Vel, .10)
wait(0.75)
ledgeavail = true
game.Debris:Destroy(Vel)
end
end
end
end
end
end)
--VaultJump System
char.Humanoid.Running:Connect(function(speed)
if speed > topspeed then
ledgeavail = true
elseif speed < topspeed then
ledgeavail = false
end
while game:GetService("RunService").RenderStepped:Wait() do
local part = workspace:FindPartOnRay(r,char)
if part and ledgeavail then
if part.Name == "VaultJump" then
if Hum.FloorMaterial ~= Enum.Material.Air then
if speed < topspeed then
ledgeavail = false
local Vel = Instance.new("BodyVelocity")
Vel.Parent = HRP
Vel.Velocity = Vector3.new(0,0,0)
Vel.MaxForce = Vector3.new(1,1,1) * math.huge
Vel.Velocity = HRP.CFrame.LookVector * 90 + Vector3.new(0,30,0)
CA:Play()
game.Debris:AddItem(Vel, .10)
wait(0.75)
ledgeavail = true
game.Debris:Destroy(Vel)
end
end
end
end
end
end)
--WallUp System
local CA = Hum:LoadAnimation(script:WaitForChild("WallUpAnim"))
char.Humanoid.Running:Connect(function(speed)
if speed > topspeed then
ledgeavail = true
elseif speed < topspeed then
ledgeavail = false
end
while game:GetService("RunService").RenderStepped:Wait() do
local part = workspace:FindPartOnRay(r,char)
if part and ledgeavail then
if part.Name == "Wallup" then
if Hum.FloorMaterial ~= Enum.Material.Air then
if speed < topspeed then
ledgeavail = false
local Vel = Instance.new("BodyVelocity")
Vel.Parent = HRP
Vel.Velocity = Vector3.new(0,0,0)
Vel.MaxForce = Vector3.new(1,1,1) * math.huge
Vel.Velocity = HRP.CFrame.LookVector * 0 + Vector3.new(0,55,0)
CA:Play()
game.Debris:AddItem(Vel, 0.1)
wait(0.70)
ledgeavail = true
game.Debris:Destroy(Vel)
end
end
end
end
end
end)
-- barrier system
local plr = game:GetService("Players").LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local HRP = char:WaitForChild("HumanoidRootPart")
local Hum = char:WaitForChild("Humanoid")
local CA = Hum:LoadAnimation(script:WaitForChild("BarrierAnim"))
while game:GetService("RunService").RenderStepped:Wait() do
local r = Ray.new(HRP.Position, HRP.CFrame.LookVector * 7 + HRP.CFrame.UpVector * -5)
local part = workspace:FindPartOnRay(r,char)
if part and ledgeavail then
if part.Name == "Barrier" then
if Hum.FloorMaterial ~= Enum.Material.Air then
ledgeavail = false
local Vel = Instance.new("BodyVelocity")
Vel.Parent = HRP
Vel.Velocity = Vector3.new(0,0,0)
Vel.MaxForce = Vector3.new(1,1,1) * math.huge
Vel.Velocity = HRP.CFrame.LookVector * 33 + Vector3.new(0,20,0)
CA:Play()
game.Debris:AddItem(Vel, .15)
wait(0.75)
ledgeavail = true
end
end
end
end

2 comments
it might be because your creating lots of Rays
so what should i do?