I was using a .Touched
event to give EXP to all players who damaged my mob. Here’s how it works:
A .Touched
event is fired, then the script checks if the colliding part is called “Handle”, with its Parent
being a child of a Model
. However, this is not important to the issue. What I’ve noticed is that when the mob gets close to a player character or when the mob dies, there are extreme frame drops. Usually to the point of bringing my 60 FPS (as there is an FPS cap) down to around 20.
At first I thought it might be an issue with my AI, so I disabled my AI script. The frame drops continued. I then tried disabling my EXP script, the one with the .Touched
event. The frame drops disappeared. I optimized all my code inside the event, yet the frame drops did not go away. Commenting all the code inside the connected function (which made the connected function empty) still left frame drops, so it was easy to assume that the event was called too often, causing the game to lag even when the connected function was empty. Is there any alternatives I could use to avoid this? Sorry if I have explained badly.
EDIT: Here’s my code, I added comments to make it easier to understand.
local playersWhoHitMe = {} --Table for all players who damaged the mob. All players, who have damaged the mob at least once will get equal amounts of EXP as everyone else.
local playerIsInTheList = false
local expAmount = math.random(45, 65)
local insert = table.insert --A little variable I'm using for syntactic sugar
script.Parent.Touched:Connect(function(hit)
if hit.Name == "Handle" and hit.Parent.Parent:IsA("Model") then --This should be self-explanatory
local model = hit.Parent.Parent
local player = game.Players:GetPlayerFromCharacter(model) --Gets the player instance
if player then --If there actually is a player then:
for i = 1, #playersWhoHitMe do --This for loop checks if the player is already in the list.
playerIsInTheList = false
if playersWhoHitMe[i].Name == player.Name then
playerIsInTheList = true
end
end
if playerIsInTheList == false then --If the player is not in the list, their name is inserted to the table.
insert(playersWhoHitMe, player)
end
end
end
if hit.Parent:FindFirstChild("Owner") then --This is the same thing as the if statement before, except it's to make it count player abilities. I should probably put the below code in a function, so that I only have to make changes once instead of in both if statements.
local model = hit.Parent
local player = game.Players:FindFirstChild(model.Owner)
if player then
for i, v in pairs(playersWhoHitMe) do
playerIsInTheList = false
if v.Name == player.Name then
playerIsInTheList = true
end
end
if playerIsInTheList == false then
insert(playersWhoHitMe, player)
end
end
end
end)
EDIT 2: To clarify, even running a completely empty function with my .Touched
event in my mob causes these frame drops.
EDIT 3 (3/8/2019): Here’s a little test place I prepared, containing only the lag-causing script. The onTouched()
function is mostly commented out, but you could uncomment it and experiment. To experience the frame-drops, try pushing the mob from the front (the side which is facing you when you press play). The amount of touches each second is printed in the output (Thanks, @IdiomicLanguage). I’ll try using Region3s and I’ll report the results.
MiniblinTestPlace.rbxl (696.7 KB)
Note: I removed most MeshID
s to prevent mesh theft.