The .Touched event tends to be very unreliable and usually only works with moving parts. In your case, you might want to consider using other methods to detect which parts have entered your Hit zone.
I would recommend trying Region3s but in your case you could alternatively just find which parts are within a certain distance from the position of your explosion.
From what I can assume, part is a predefined variable set to nothing.
In programming, there’s a concept known as scope/ local scope/ etc. Due to how events and functions are handled on the back end, if you make a variable in a function then it has local scope to that function. There are a few better explanations, one being here.
However, you may then say “but I made the variable outside of the event, so that doesn’t matter!”, but sadly, it does. Due to how events are processed, once again, it doesn’t run the function provided to it until it’s fired, meaning part doesn’t exist until it’s made. Because of this, the part variable is nil, and nil has no touched event, which is why nothing is happening.
There are a few ways you can go about it. I’m going to go on the basis that it’s all being done within the first function, however.
For example, you can use a rayCast to figure out where its going to hit, when, etc however then you’ll have to use another method to get the parts within the area, as such of a Region or magnitude as @cosmental rightfully stated.
Another way would be to use a stepped or heartbeat loop and increase it’s position yourself, to where you can then cast smaller rays which are more accurate. This is harder and arguably less efficient but means if any parts intend to move it won’t hit them if they move out of the way, unlike the first example.
A final way which I don’t tend to recommend is using the touched event within the original for the part, but this is only a last resort sort of move and I don’t tend to like doing as such.
You don’t need to re-reference the part? You’re making those pieces of code within the original event where it was called so you should already have access to it
for i,v in pairs(workspace:GetDescendants()) do
if v:IsA("BasePart") and v.Name ~= "BasePlate" then
mag = (script.Parent.Position - v.Position).Magnitude
if mag < 5 and mag>-5 then
--do ur stuff
end
end
end
Basically, when you have 2 vectors, the quickest line from point a to point b is the magnitude, and it has a base unit of 1 stud in roblox. To get the magnitude, you do (VectorA - VectorB).Magnitude, which will return the length of that line, i.e 15.
PS - It may be VectorB - VectorA, I’m a little rusty on the maths so do correct me if I’m wrong.
This code is completely wrong as .Magnitude is not a valid property of Position.
The proper and practical way to go about this would be as follows:
for _, Object in pairs(workspace:GetDescendants()) do
if (not Object:IsA("BasePart")) then continue; end
local Magnitude = (ExplosionPart.Position - Object.Position).Magnitude
if (Magnitude >= Object.Size.X) then -- This if statement assumes that your X, Y, and Z size inputs are all the same
-- Unanchor your "Object" instance
end
end
The ball flung out of existence or something, here’s the script:
for i,v in pairs(workspace:GetDescendants()) do
if v:IsA("BasePart") and v.Name ~= "BasePlate" then
mag = (script.Parent.Position.Magnitude - v.Position.Magnitude)
if mag < 5 and mag>-5 then
local hit = v
if hit:IsA("Part") and hit.Name ~= "Invis" then
hit.Anchored = false
hit.Position = hit.Position + Vector3.new(1,0,0)
hit.Position = hit.Position + Vector3.new(1,0,0)
local tweenservice = game:GetService("TweenService")
part = Instance.new("Part")
part.Shape = Enum.PartType.Ball
part.Size = Vector3.new(0.01,0.01,0.01)
part.Position = script.Parent.Position
part.BrickColor = BrickColor.new("Deep orange")
part.CanCollide = true
part.Anchored = true
part.Transparency = 0.4
part.Parent = workspace
local info = TweenInfo.new(0.2, Enum.EasingStyle.Linear, Enum.EasingDirection.Out, 0, false, 0)
local goal = {
Size = Vector3.new(83.8, 83.8, 83.8)
}
local create = tweenservice:Create(part, info, goal)
create:Play()
script.Parent:Destroy()
wait(0.7)
part:Destroy()
end
end
end
end