I am creating a wave-based game and I have a grenade ability. At about 75% of the way through the game, my frame rate drops from a consistent 60 FPS to 35-45 FPS for the remainder of play. If I complete the game without using the grenade ability at all, this never seems to happen, so I am pretty confident it is the cause.
I have a local script for the input (removed some parts that don’t apply):
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Timestamp1 = tick()
local UserInputService = game:GetService("UserInputService")
local animation2 = Instance.new("Animation")
animation2.AnimationId = "http://www.roblox.com/asset/?id=7148111065"
local animationTrack2 = character:WaitForChild("Humanoid"):LoadAnimation(animation2)
UserInputService.InputBegan:Connect(function(Input, IsTyping)
if not IsTyping then
if Input.KeyCode == Enum.KeyCode.Q then
if character.Class.Value == "Pistol" then
if (tick() - Timestamp1) < (character.Q.Value) then return end --Q is an int value inside of character
animationTrack2:Play()
Timestamp1 = tick()
wait(0.2)
game.ReplicatedStorage.RemoteEvents.Grenade:FireServer()
end
end
end
end)
Here is the server-side script: (I have tried many updates in trying to reduce this lag)
local grenade = game.ServerStorage:WaitForChild("Grenade")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local debris = game:GetService("Debris")
game.ReplicatedStorage.RemoteEvents.Grenade.OnServerEvent:Connect(function(player)
local character = player.Character
local newgrenade = Grenade:Clone()
newgrenade.CFrame = character.HumanoidRootPart.CFrame + Vector3.new(0,3,0)
newgrenade.Parent = workspace
newgrenade.CanCollide = true
newgrenade.Velocity = (character.HumanoidRootPart.CFrame.lookVector * 75)
newgrenade.Pin:Play()
wait(1.5)
local explosion = Instance.new("Explosion", workspace)
explosion.Position = newgrenade.Position
explosion.BlastRadius = player.Character.QRad.Value --another int variable inside character
explosion.BlastPressure = 250000
explosion.DestroyJointRadiusPercent = 0
local maxDamage = player.Character.QDam.Value --another int variable inside character
local modelsHit = {}
-- listen for contact
explosion.Hit:Connect(function(part, distance)
local parentModel = part.Parent
if parentModel then
-- check to see if this model has already been hit
if modelsHit[parentModel] then
return
end
-- log this model as hit
modelsHit[parentModel] = true
-- look for a humanoid
local humanoid = parentModel:FindFirstChild("Humanoid")
if humanoid then
local chartar = humanoid.Parent
local plrtar = Players:GetPlayerFromCharacter(chartar) --ensure only damages npcs
if not plrtar then
humanoid:TakeDamage(maxDamage)
end
end
end
end)
newgrenade:Destroy()
end)
I have tried adding in ways to disconnect from the explosion.Hit, but it always prevents me from throwing another grenade or doing damage with subsequent grenades. Not sure how else I could be leaking memory.
edit: I also tried changing the script to parent the explosion after creating it because I heard that can cause issues in regards to performance, but I am still experiencing the frame rate drop.