So I’m working on this fighting game where it’s main function of attacking is usually spawning parts that serve at attacks like a bone that flies out wherever you are facing or more specifically in this case the occasional bomb/AoE attack that usually consists of a sphere Union with PreciseConvexDecomposition set on it’s CollisionFidelity to make it’s hitbox as accurate to the sphere itself as possible
The problem I’ve been experiencing lately with the big sphere AoE attacks though is that if the Player is standing directly inside of the sphere attack it will drop their FPS down significantly until they’re either out of the sphere or the sphere attack expires, I have not seen this kind of lag happen with any other type of attack. Essentially what I’m looking to do is to try and hopefully get rid of that huge lagspike that happens when you’re inside of the sphere attack while it’s active, and I have reason to believe that the lag is coming from the way the damage script for the attack itself is programmed.
Here is the code from one of said sphere attacks:
local HealthLoss = script.Parent.Parent.dmg.Value -- The amount of damage the attack does
function OnTouched(Part)
if script.Parent.Parent.owner.Value ~= Part.Parent then
if Part.Parent ~= nil then
if Part.Parent:findFirstChild("Humanoid") ~= nil then
Part.Parent:findFirstChild("Humanoid"):TakeDamage(HealthLoss) -- Damages
if Part.Parent.Humanoid.Health <= 0 and Part.Parent.Humanoid:findFirstChild("ded") == nil then -- Kill reward, adds an IntValue to the target's Humanoid to check that it's already been killed so it doesn't reward the 'SOULs' value more than once
local die = Instance.new("IntValue", Part.Parent.Humanoid)
die.Name = "ded"
local p = game.Players:GetPlayerFromCharacter(script.Parent.Parent.owner.Value)
p.leaderstats.SOULs.Value += (1*(p.leaderstats["SOUL Mult"].Value))
end
end
end
end
end
script.Parent.Touched:connect(OnTouched)
I have at some point during this game’s very early development tried adding a task.wait() to the script to maybe try and fix some of the lag, but that would prevent the attack itself from being able to damage multiple targets at once
I’ve also set every attack part in the game (Even the melee weapons) to be Massless to see if it was an issue with the mass of the Union itself but it didn’t really seem to provide any noticeable changes
Keep in mind that I do want it to still do damage per tick (or something slower than that, I will understand if it’s for the sake of removing some of the lag) and I do also want it to be able to damage multiple targets at once (Like if there’s three people within the range of the sphere, all of them will take damage from it at once)
Any and all help on this issue will be greatly appreciated, thank you ! ^ ^
Ok, well this may or may not fix the lag, but I would recommend not using part.Touched and instead using Workspace:GetPartsInPart() in a RunService connection, as this is more accurate than touched and also uses proper query functions.
Additonally, I see your checking if a IntValue exists as a child to check if something is dead. A better way of doing this is using Tags.
Here is how using tags with your code could look (specifically the 4th nested if statement.):
if Part.Parent.Humanoid.Health <= 0 and not Part.Parent.Humanoid:GetTag("ded") then -- Now uses a tag to check if the player is dead so the "Souls" don't get reawarded.
Part.Parent.Humanoid:AddTag("ded")
local p = game.Players:GetPlayerFromCharacter(script.Parent.Parent.owner.Value)
p.leaderstats.SOULs.Value += (1*(p.leaderstats["SOUL Mult"].Value))
end
Also is there any tweening on the explosion and is it a complex model?
Additionally, since the explosion radius is a sphere anyway, why not just use Workspace:GetPartBoundsInRadius() instead? This would also presumably save on performance issues, as the sphere attack would become purely decorative and not require setting to PreciseConvexDecomposition. Of course, this doesn’t work if the attack is anything other than a sphere or cuboid (if it’s a cuboid you should use Workspace:GetPartBoundsInBox() instead). Workspace:GetPartsInPart() would be superior if your character hitboxes are cylindrical or spherical in shape though, since the other functions I listed just check the bounding boxes of parts instead of their true geometry.
I mean that would work too I Guess. Its just the fact that there is a specific mesh part (and also I assume there might be tweening) so I was using the generic one that uses a part input, but those are also good and also more efficient if the hitbox is a unmoving circle/cuboid.
To answer the question about the explosion, no it does not have tweening, it is animated solely using CFrame
And it isn’t really a complex model either, it’s just a sphere
Also just now I tried both GetPartsInPart() and GetPartBoundInRadius() as suggested by JohnsonBlu, both had the same results yet different errors
It all errors on Line 7 it seems, GetPartsInPart() seems to have said “unable to cast value to object” from what I remember, and GetPartBoundInRadius() just now gave the error message of “Unable to cast CoordinateFrame to Vector3”?
Here is the code that I used, this code uses GetPartBoundInRadius() but the last code was essentially the same just replace that with GetPartsInPart():
local HealthLoss = script.Parent.Parent.dmg.Value
local CFrame = script.Parent.CFrame
local size = script.Parent.Size --Matches with the explosion's current size
local params = nil
local PartsArray = workspace:GetPartBoundsInRadius(CFrame, size, params) --Line 7
for i, Part in ipairs(PartsArray) do
if script.Parent.Parent.owner.Value ~= Part.Parent then
if Part.Parent ~= nil then
if Part.Parent:FindFirstChild("Humanoid") ~= nil then
Part.Parent:FindFirstChild("Humanoid"):TakeDamage(HealthLoss)
if Part.Parent.Humanoid.Health <= 0 and Part.Parent.Humanoid:findFirstChild("ded") == nil then
local die = Instance.new("IntValue", Part.Parent.Humanoid)
die.Name = "ded"
local p = game.Players:GetPlayerFromCharacter(script.Parent.Parent.owner.Value)
p.leaderstats.SOULs.Value += (1*(p.leaderstats["SOUL Mult"].Value))
end
end
end
end
end
I didn’t add the tag thing yet since I wanted to try out those functions first before I delved into that, I used the same code from a certain attack I asked for help from the DevForums on at an earlier time (Thank you freezekill123 and Kira Fietowski ! ^ ^) where the Tool itself called for GetPartBoundsInBox() so it could get every NPC in radius so it would create an attack behind them since I thought they would work quite similarly
The reason GetPartInBoundsRadius Is complaining is because you’re giving a CFrame when its asking for a Vector3.
You can find that out from the error it provided “Unable to cast CoordinateFrame To Vector3”, where its basically saying. “You gave me a CFrame when I need a Vector3, but I’ll try to convert it anyway. Nope I can’t convert it.” Then the code cries.
For get GetPartsInPart, it uses an actual instance of a part. (So that means you can’t put a CFrame in there too!)
You can also figure this out from the error message which you should be able to understand now.
Though when I try to remove the CFrame part of the code now it’s erroring saying “Argument 2 missing or nil”, I have a feeling that something needs to go there but I’m not sure what
Maybe I need to put script.Parent.Size in place of CFrame? But no because that’s already accounted for in the middle part of the code
Here is the part of the code that has been changed:
local size = script.Parent.Size
local params = nil
local PartsArray = workspace:GetPartBoundsInRadius(size, params)
Alternatively just now I did try to put ‘Vector3’ in place of CFrame but now it’s erroring saying “Unable to cast Dictionary to Vector3”
You still need a vector3 there, its basically now saying that there is something missing while before it was saying the thing you put in was wrong. You there is 2 ways to correctly do this. (Also You can’t just put Vector3 there since you don’t have a variable for vector3 which means you’re actually getting the Vector3 class)
Here is the first way you could do that line of code: local PartsArray = workspace:GetPartBoundsInRadius(CFrame.Position, size, params)
Or you could do this:
local ParentCFrame = script.Parent.CFrame -- You shouldn't name the variable to be the exact same as the class as it may cause issues (but make sure to rename all uses of the variable otherwise everything will break)
local ParentPosition = script.Parent.Position
local size = script.Parent.Size --Matches with the explosion's current size
local params = nil
local PartsArray = workspace:GetPartBoundsInRadius(ParentPosition, size, params)
Both work but for brevity I would recommend the first one.
local HealthLoss = script.Parent.Parent.dmg.Value
local CFrame = script.Parent.CFrame
local size = script.Parent.Size
local params = nil
local PartsArray = workspace:GetPartBoundsInRadius(CFrame.Position, size, params)
for i, Part in ipairs(PartsArray) do
if script.Parent.Parent.owner.Value ~= Part.Parent then
if Part.Parent ~= nil then
if Part.Parent:FindFirstChild("Humanoid") ~= nil then
Part.Parent:FindFirstChild("Humanoid"):TakeDamage(HealthLoss)
if Part.Parent.Humanoid.Health <= 0 and Part.Parent.Humanoid:findFirstChild("ded") == nil then
local die = Instance.new("IntValue", Part.Parent.Humanoid)
die.Name = "ded"
local p = game.Players:GetPlayerFromCharacter(script.Parent.Parent.owner.Value)
p.leaderstats.SOULs.Value += (1*(p.leaderstats["SOUL Mult"].Value))
end
end
end
end
end
Ah, I see. The Size in GetBoundsInRadius() isn’t a vector3, its a float / number. It determines how big the sphere is, since spheres are equally sized in all directions. You should be able just to use any of the single size axis, for example Size.X as its a sphere. If for some reason all numbers aren’t the same on the sphere, just use the most accurate looking one.
Basically just do this: local PartsArray = workspace:GetPartBoundsInRadius(CFrame.Position, size.X, params)
Well it’s no longer giving any errors in the console, but it’s still not doing any damage to any of the NPCs
I think with that yellow spear attack that used a similar function, it created that hitbox for a split second before disappearing, I’m pretty sure that that might be what’s happening with the GetPartBoundsInRadius() for this explosion attack since the thing with the yellow spear used GetPartBoundsInBox() which is a very similar function
The question being though, how do I get the hitbox to not disappear for the explosion itself (At least not until the attack has finished)?
Well GetBoundsInRadius and GetBoundsInBox() won’t give a visual hitbox, and unless you create a visual yourself you won’t be able to see it, but as far as it goes it should just be the same as whatever the bomb explosion size is.
Also the reason its not working is that you forgot to put that for loop into a RunService() connection, which will make that loop run everyframe. Currently it runs once, and there is a high chance you aren’t being detected in that one loop.
Here, this is fully edited code that should make this run EVERY frame:
local HealthLoss = script.Parent.Parent.dmg.Value
local RunService = game:GetService("RunService")
local CFrame = script.Parent.CFrame
local size = script.Parent.Size
local params = nil
RunService.Heartbeat:Connect(function()
local PartsArray = workspace:GetPartBoundsInRadius(CFrame.Position, size, params)
for i, Part in ipairs(PartsArray) do
if script.Parent.Parent.owner.Value ~= Part.Parent then
if Part.Parent ~= nil then
if Part.Parent:FindFirstChild("Humanoid") ~= nil then
Part.Parent:FindFirstChild("Humanoid"):TakeDamage(HealthLoss)
if Part.Parent.Humanoid.Health <= 0 and Part.Parent.Humanoid:findFirstChild("ded") == nil then
local die = Instance.new("IntValue", Part.Parent.Humanoid)
die.Name = "ded"
local p = game.Players:GetPlayerFromCharacter(script.Parent.Parent.owner.Value)
p.leaderstats.SOULs.Value += (1*(p.leaderstats["SOUL Mult"].Value))
end
end
end
end
end
end)
At first it was running an error on the PartsArray part of the code again but that was because it was missing the size.X thing so that was a quick fix
But then i tried it after the fact and the first time it didn’t do any damage to anybody
Another time I tried it and it only manage to kill one NPC (I got the EXP reward from it still) but didn’t even damage any of the other NPCs that were inside the explosion radius
Then I tried it one more time and it went back to doing nothing