My friends have said my game is laggy, and one of the places it seems to lag the most is at a lava chase a bit further into the game. When they get there, their FPS continuously spikes from normal FPS to super low FPS very rapidly. I looked through the code and can’t find anything that would lag the game this bad. Is there anything I could change to make it less laggy?
Code snippet of all relevant code:
---------------------------------------------------------------------------------------------------------------
-- SHAKE
function CamShake(decay,offsetmin,offsetmax,playsound,customsound,volume,subject)
spawn(function()
local decay = decay*.1
local volume = volume*.1
local snd = Instance.new("Sound")
local vol = volume
if customsound == false then
snd.SoundId = "rbxassetid://153274423"
else
snd.SoundId = "rbxassetid://".. tostring(customsound)
end
snd.Pitch = math.random(90,110) * .01
snd.Volume = vol
snd.Parent = script
game.Debris:AddItem(snd, 3)
if playsound == true then
snd:Play()
end
local fin = tick() + decay
local savedpos = nil
if subject then
if subject == true then
savedpos = Camera.CFrame
else
savedpos = subject.Position
end
end
while game:GetService("RunService").RenderStepped:wait() and (fin-tick() > 0) and hum.Health > 0 do
snd.Volume = vol * (fin-tick())
if subject then
if subject == true then
Camera.CFrame = Camera.CFrame + Vector3.new( ((math.random(offsetmin,offsetmax)*.05) * (fin-tick())),
((math.random(offsetmin,offsetmax)*.05) * (fin-tick())),
((math.random(offsetmin,offsetmax)*.05) * (fin-tick()))
)
else
subject.Position = subject.Position + Vector3.new( ((math.random(offsetmin,offsetmax)*.05) * (fin-tick())),
((math.random(offsetmin,offsetmax)*.05) * (fin-tick())),
((math.random(offsetmin,offsetmax)*.05) * (fin-tick()))
)
end
else
hum.CameraOffset = Vector3.new( ((math.random(offsetmin,offsetmax)*.05) * (fin-tick())),
((math.random(offsetmin,offsetmax)*.05) * (fin-tick())),
((math.random(offsetmin,offsetmax)*.05) * (fin-tick()))
)
end
end
if subject then
if subject == true then
Camera.CFrame = savedpos
else
subject.Position = savedpos
end
end
end)
end
hum.Died:connect(function() hum.CameraOffset = Vector3.new() end)
---------------------------------------------------------------------------------------------------------------
-- EVENTS+
function Recheck()
---------------------------------------------------------------------------------------------------------------
-- EVENT 2
spawn(function()
task.wait()
local debounce2 = true
local enddebounce = true
local Event = workspace:WaitForChild("Event2")
local Trigger = Event:WaitForChild("Trigger2")
local Barrier = Event:WaitForChild("Barrier")
local Lava = Event:WaitForChild("Lava")
local CamPart = Event:WaitForChild("CamPart")
local LavaLight = Lava:WaitForChild("FrontLight")
local EndTrigger = Event:WaitForChild("EndTrigger")
local EndBarrier = Event:WaitForChild("EndBarrier")
local CloseDoor = Event:WaitForChild("CloseDoor")
local CloseDoorSound = CloseDoor:WaitForChild("MetalPipe")
local ChaseMusic = script.ChaseMusic
local ChaseMusicChoice = 1
local Tween1 = TS:Create(Lava, TweenInfo.new(0.5), {Position = Vector3.new(-0, 25.5, 56.75)}) -- 58.5 studs up
local Tween2 = TS:Create(Lava, TweenInfo.new(60, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0), {Position = Vector3.new(-0, 25.5, -343.25)}) -- 300 studs forward
local Tween3 = TS:Create(ChaseMusic, TweenInfo.new(2.5), {Volume = 1})
local Tween4 = TS:Create(ChaseMusic, TweenInfo.new(3), {Volume = 0})
local Tween5 = TS:Create(CloseDoor, TweenInfo.new(2, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out), {Position = CloseDoor.Position + Vector3.new(0, -49, 0)})
local Event2Tweens = {Tween1, Tween2, Tween3, Tween4, Tween5}
local function TriggerFunc()
print("hi")
Barrier.CanCollide = true
for i, v in pairs(Music:GetChildren()) do
if v.ClassName == "Sound" then
local Tween0 = TS:Create(v, TweenInfo.new(3), {Volume = 0})
table.insert(Event2Tweens, Tween0)
Tween0:Play()
end
end
CamShake(20,-5, 5, true, false, 20)
task.wait(1)
Camera.CameraType = Enum.CameraType.Scriptable
Camera.CFrame = CamPart.CFrame
task.wait(2)
script.LavaExplosion:Play()
CamShake(20,-5, 5, true, false, 8, true)
Tween1:Play()
task.wait(2)
Tween2:Play()
task.wait(0.5)
Camera.CameraType = Enum.CameraType.Custom
Barrier.Transparency = 0.9
ChaseMusic.Volume = 0
if ChaseMusicChoice == 1 then
ChaseMusic.SoundId = "rbxassetid://1836136972"
ChaseMusic.TimePosition = 6
ChaseMusic.PlaybackSpeed = 1.2
else
ChaseMusic.SoundId = "rbxassetid://1837301393"
ChaseMusic.TimePosition = 5
ChaseMusic.PlaybackSpeed = 1
end
ChaseMusic:Play()
Tween3:Play()
task.wait(2.3)
Barrier.CanCollide = false
Barrier.Transparency = 1
end
Trigger.Touched:Connect(function(hit)
local h = hit.Parent:FindFirstChild("Humanoid")
if h and h == hum and debounce2 then
debounce2 = false
EventNum = 2
local thread = task.spawn(TriggerFunc)
hum.Died:Connect(function()
task.cancel(thread)
end)
end
end)
local function EndTriggerFunc()
EndBarrier.CanCollide = true
Tween4:Play()
task.wait(0.2)
Tween5:Play()
task.wait(0.6)
CloseDoorSound:Play()
local Tween6 = TS:Create(LavaLight, TweenInfo.new(1), {Brightness = 0})
table.insert(Event2Tweens, Tween6)
Tween6:Play()
task.wait(4)
for i, v in pairs(Music:GetChildren()) do
if v.ClassName == "Sound" then
local Tween7 = TS:Create(v, TweenInfo.new(2), {Volume = MusicVol})
table.insert(Event2Tweens, Tween7)
Tween7:Play()
end
end
while task.wait() do
if localplayer:GetAttribute("CheckpointNumber") > 4 then
EventNum = 0
print("EventNum: ".. EventNum)
break
end
end
end
EndTrigger.Touched:Connect(function(hit)
local h = hit.Parent:FindFirstChild("Humanoid")
if h and h == hum and enddebounce then
enddebounce = false
local thread = task.spawn(EndTriggerFunc)
hum.Died:Connect(function()
task.cancel(thread)
end)
end
end)
localplayer.CharacterAdded:Connect(function(ch)
if EventNum == 2 then
ChaseMusicChoice = math.random(1, 2)
if localplayer:GetAttribute("CheckpointNumber") < 5 then
for i, v in pairs(Event2Tweens) do
v:Cancel()
end
Lava.Position = Vector3.new(0, -62.25, 56.75)
Barrier.CanCollide = false
Barrier.Transparency = 1
EndBarrier.CanCollide = false
LavaLight.Brightness = 1
CloseDoor.Position = Vector3.new(0, 74.5, -571.75)
ChaseMusic.Volume = 0
ChaseMusic:Stop()
debounce2 = true
enddebounce = true
end
EventNum = 0
end
end)
end)
end
---------------------------------------------------------------------------------------------------------------
-- RECHECK
local eventthread = task.spawn(Recheck)
game:GetService("Players").PlayerAdded:Connect(function(Player)
Player.Chatted:Connect(function(Message) -- RECHECK COMMAND
if Player.Name == localplayer.Name and Message == "/recheck" then
task.cancel(eventthread)
eventthread = task.spawn(Recheck)
end
end)
end)