Touched function does not work after cloning in my placement script?

Hello everyone, I am currently working on my placement module but I have encountered a problem. The title explains it all. Tell me if I need to add more info.
Additional Info:

  • I have tried to un-anchor it (and weld it) and anchor it → no result

  • I have created by making a server placement and locally place it → no result

  • The part I want to collide is not a primary part

Here is my script:

local p = script.Parent
print("called") --this works perfectly

script.Parent.Touched:Connect(function(part) --this doesn't get called
	if part.Name == "Checker" then
		print("touched")--this doesn't get called
	else 
		print("untouched")-- or this doesn't get called
	end
end)

If somebody can help me find a solution or explain why this problem occurs; I would appreciate it.

Thanks for Helping!

1 Like

I suggest putting a print after the function so you can test if it works; check if the part is anchored.
and what’s the “Checker” supposed to be?
Give us more detail and screenshots of tools or anything you use, you’re talking about unanchoring it while we don’t even know what it is, so the things I just talked about were just guesses.

1 Like

I have already tried putting a print after the function, and Checker is the part that the script is supposed to be checking.

1 Like

No, I mean inside and after the function so:

script.Parent.Touched:Connect(function(part) --this doesn't get called
	print("check if touched")
	if part.Name == "Checker" then
		print("checker touched")--this doesn't get called
	else 
		print("other part touched")-- or this doesn't get called
	end
end)
1 Like

Yes, I already tried that but it does not work.

1 Like

Maybe is not getting touched…?

1 Like

Then the problem lays with script.Parent turning to nil

1 Like

U should try setting the part’s parent to workspace. This might be a result of roblox not doing any physic based events and physics itself outside of workspace

The highlighted part is what I am trying to check i collided
image

The video:

The result:
image

The result is supposed to say “touched” or “untouched”

Ask if I need to add more info.

Give us the script for the cloned checker

The checking Script:

local p = script.Parent
print("called")

script.Parent.Touched:Connect(function(part)
	if part.Name == "Checker" then
		print("touched")
	else 
		print("untouched")
	end
end) 	

The server placement script:

local re = game.ReplicatedStorage:WaitForChild("ServerPlacement")
local folder = game.Workspace

local function place(player,part, pos)	
	p = part:Clone()
	p.Parent = folder --game.Workspace	
	p:SetPrimaryPartCFrame(pos)
end

re.OnServerEvent:Connect(place)
1 Like

Make sure p is named Checker? Otherwise Checker does not exist

Yes p - is named correctly @ThatLazyBuilder .

Hmm try if part.Name == p.Name then

Unfortunately, that did not work; my problem is that nothing is getting called inside the touched function.

After Testing it, it seems like it only fires when i drop the checker on top of another checker. . Kinda like when you stood still on top of the part without moving, the event does not fire. The event only fires when the part that is being hit is moving.

So the part has to be moving in order for it to detect aswell.

The Touched event is only called as a result of physics movement. The way you are moving it here seems to be by manually changing the CFrame of the part. You can read more about the specifics of the Touched event here.

“This event only fires as a result of physics movement, so it will not fire if the CFrame property was changed such that the part overlaps another part. This also means that at least one of the parts involved must not be BasePart.Anchored at the time of the collision.”

If you walked your character over the cloned parts the Touched event would fire in those scenarios.

An alternative solution to what you’re trying to do is using a Region3 when the parts position is moved. I’ll keep as much of your original code in my example as possible.

local p = script.Parent

script.Parent:GetPropertyChangedSignal("Position"):Connect(function()
	local min = p.Position - p.Size / 2
	local max = p.Position + p.Size / 2
	local partRegion = Region3.new(min, max)

	local partsInRegion = workspace:FindPartsInRegion3(partRegion)
	for _, part in pairs(partsInRegion) do
		if part.Name == "Checker" then
			print("touched")
		else
			print("untouched")
		end
	end
end)
3 Likes

Ahh something new everyday. I Didnt know this I was trying to explain the result in my experiment and it explained exactly what you stated.

Thank you everyone who helped me!