Touch not detecting

I’ve been posting in this category a lot recently for my problem. I’ve been having issues with the touch event for 3 days now.

Basically, what’s happening is that, the explosion is not unanchoring the parts. Here’s a video:

Here’s the script:


script.Parent.Touched:Connect(function(hit)
	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)

part.Touched:Connect(function(hit)
	hit.Anchored = false
	hit.Position = hit.Position + Vector3.new(1,0,0)
	hit.Position = hit.Position + Vector3.new(1,0,0)
end)
script.Parent.Touched:Connect(function(hit)
	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()
part.Touched:Connect(function(hit)
	hit.Anchored = false
	hit.Position = hit.Position + Vector3.new(1,0,0)
	hit.Position = hit.Position + Vector3.new(1,0,0)
end)
		wait(0.7)
		part:Destroy()
	end
end)

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.

Hope this helps! :grin:

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.

PS: Use task.wait :trolleybus:

This unfortunately still does not work.

I was thinking of using task.wait actually.

Wait a second, then how do I reference the part in question?

Yeah, I’ll definitely note that, I’ll try use magnitude because I heard you can use it,

1 Like

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.

Yes, I’ll try use magnitude but I don’t think you answered my question, how do you reference the part?

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

I’m struggling to use magnitude a little bit, I don’t understand how you get the part that you need to check the magnitude by.

Basically, I can’t seem to figure out which part I should subtract the magnitude by, here’s an example:

if (script.Parent.Magnitude - [Second Parameter] -- I don't know what goes here. )

you could check every part

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
1 Like

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.

I’ve tried this and unfortunately it hasn’t worked, I probably did it wrong though.

I edited it did u try it again, i accidentally put it to 1 studs away lol

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

I would recommend using my solution as Position.Magnitude would produce an error.

Object flew out of existence again!