local function Splash(targeted, obj)
targeted.Humanoid:TakeDamage(obj.Config:WaitForChild("Damage").Value)
local radius = obj.Config.SplashRadius.Value
local Enemies = workspace.Mobs:GetChildren()
local targets = {}
for i, target in pairs(Enemies) do
local distance = (targeted:WaitForChild("HumanoidRootPart").Position - target:WaitForChild("HumanoidRootPart").Position)
if distance <= radius and target ~= targeted then
table.insert(targets, target)
target.Humanoid:TakeDamage(obj.Config:WaitForChild("Damage").Value)
player.Cash.Value += obj.Config:WaitForChild("Damage").Value
end
end
end
and the error is “attempt to compare Vector3 <= number” can anyone help me on how to fix it?
local function Splash(targeted, obj)
targeted.Humanoid:TakeDamage(obj.Config:WaitForChild("Damage").Value)
local radius = obj.Config.SplashRadius.Value
local Enemies = workspace.Mobs:GetChildren()
local targets = {}
for i, target in pairs(Enemies) do
local distance = (targeted:WaitForChild("HumanoidRootPart").Position - target:WaitForChild("HumanoidRootPart").Position).Magnitude
if distance <= radius and target ~= targeted then
table.insert(targets, target)
target.Humanoid:TakeDamage(obj.Config:WaitForChild("Damage").Value)
player.Cash.Value += obj.Config:WaitForChild("Damage").Value
end
end
end
The subtraction between two vectors returns another vector. To convert it to the distance between the two original vectors you need to use the .Magnitude property of the resulting vector(that is equal to sqrt(x^2+y^2+z^2)), so you need to do:
if distance.Magnitude <= radius and target ~= targeted then
I believe the solution will work, but I’m foreseeing another aspect of the code that can be improved.
From the function name “Splash”, I assume this attack is happening on the X- and Z-axis, but not the Y-axis. Would this be correct? If so, well, the magnitude check we performed was spherical, but what we really wanna check is a cross-section of that sphere. To fix this, we can do a second check for seeing their proximity on the Y-Axis, which will effectively cut the top and bottom off of the sphere.
local function Splash(targeted, obj)
targeted.Humanoid:TakeDamage(obj.Config:WaitForChild("Damage").Value)
local radius = obj.Config.SplashRadius.Value
local Ymargin = 3 -- this is how high or below your target can be from the splash. Make it higher to make it more lenient.
local Enemies = workspace.Mobs:GetChildren()
local targets = {}
for i, target in pairs(Enemies) do
local vectordistance = (targeted:WaitForChild("HumanoidRootPart").Position - target:WaitForChild("HumanoidRootPart").Position) -- this is a vector
local distance = vectordistance.Magnitude -- this is a number
local Ydistance = math.abs(vectordistance.Y) -- number that displays how far they are from each other on the Y-axis
if distance <= radius and Ydistance < Ymargin and target ~= targeted then
table.insert(targets, target)
target.Humanoid:TakeDamage(obj.Config:WaitForChild("Damage").Value)
player.Cash.Value += obj.Config:WaitForChild("Damage").Value
end
end
end
You can discard the Y axis by multiplying with the 1, 0, 1 vector:
local distance = (targeted:WaitForChild("HumanoidRootPart").Position - target:WaitForChild("HumanoidRootPart").Position)
distance *= Vector3.new(1, 0, 1)
if distance.Magnitude <= radius and --rest of your if statement
Or you can directly calculate the distance for the specific coordinates you want:
local magnitude = math.sqrt(distance.X*distance.X + distance.Z*distance.Z)
Good point! But by doing this, aren’t we limiting the calculation to a very small plane? With my approach you can adjust the Ymargin variable to allow more flexibility in how far the splash extends upwards and downwards.
We completely ignore the Y axis(height) and act as if we’re calculating the distance on a flat 2D plane(where x and z are the coordinates). Basically we assume the world is flat.
Yep, because it’s removing the Y component. Which to be fair, is what it sounds like I was trying to express here:
But grammatically, what I was really trying to express was this:
Here I describe a cross-section that can vary in height from 0 to the full extent of the radius.
If Ymargin is set to zero, it will be completely flat, just like a (1, 0, 1) vector. If Ymargin is set to radius, it’s just gonna be the full spherical magnitude check. But if Ymargin is equal to, say, radius/4, it’ll give us a 1/4 cross-section of the sphere, 1/8 from the diameter of the sphere in both directions, aligned to the X-axis. Which is what I described here:
With the (1, 0, 1) vector multiplication approach, the targeted entity could be 0.01 studs above the splash and be completely ignored. But by having the Ymargin variable approach employed instead, you can directly control how permissive the check is in the Y-axis.