How do i detect if a part is touching another

script.Parent.Touched:connect(function(hit)
	if hit.Name ~= "Sausage" then
		local part = workspace.Sausage
		print("Cooking")
		part.Color = Color3.fromRGB(124, 65, 64)
		print("Done")
	else
		print("Not sausage")
	end
end)

when i made it a server script any time i touched the part then the colour would change even tho i want the part named sausage to change colour when ever it touches the part and when i made it a local script OFC it would not work.

I think you meant to do this:

script.Parent.Touched:Connect(function(hit)
	if hit.Name == "Sausage" then -- if name of 'hit' part is "Sausage" (replaced '~=' with '=='
		local part = workspace.Sausage
		print("Cooking")
		part.Color = Color3.fromRGB(124, 65, 64)
		print("Done")
	else -- if not
		print("Not sausage")
	end
end)

You were checking if the hit part was not a part named ‘Sausage’ (symbols: ~=), which is likely why you are facing this issue.

1 Like
if table.find(part:GetTouchingParts(), otherpart) then
    -- code
end
script.Parent.Touched:Connect(function(hit)
	if hit.Name == "Sausage" then
		local part = workspace.Sausage
		print("Cooking")
		part.Color = Color3.fromRGB(125,65,64)
		print("Done")
	else
		print("Not sausage")
	end
end)

Im pretty sure you didnt mean to say ~= and meant ==, ~= means not, == means is basically. If you wanted to do multiple sausages, you could also remove the

local part = workspace.Sausage

and just do

hit.Color = Color3.fromRGB(124, 65, 64)

That would make it so whenever any sausage hits the cooker (im assuming its a cooking device) it would cook instead of only 1 being able to cook. Best of luck!

1 Like

Thank You, i must have been sleeping while coding cuz i didnt notice that little symbol!

2 Likes

You’re welcome. Also, I believe the solution should go to my post as I (was the first to) state and fix the main issue of your code.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.