So I am trying to make a building system, with snapping, but the snap parts are not deleted when I fire the script, can anyone help me?

for _,q in pairs(game.Workspace:GetDescendants()) do
	if q:IsA("Part") then
		for _,s in pairs(game.Workspace:GetDescendants()) do
			if s:IsA("Part") then
				if s.Position == q.Position and s~= q then
					if string.sub(s.Name,1,1) == "e" and string.sub(q.Name,1,1) == "SP" then
						q:Destroy()
					elseif string.sub(q.Name,1,1) == "e" and string.sub(s.Name,1,1) == "SP" then
						s:Destroy()
					else
						local r= math.random(0,1)
						if r== 0 then
							s:Destroy()
						else
							q:Destroy()
						end
					end
				end
			end
		end
	end
end

the place is on my profile, its called "Random Testing Environment"Link

7 comments

0

oh i forgot to say run the code in command line

0

and the script deletes parts overlapping each other

0

whats with the substring stuff? Also you are checking if one character is equal to 2 characters while also being equal to another different character, so that's probably your problem there

0

substring means it checks the character, in this case it is checking the first character of the string, hence why it is substring(q.Name,1,1))

0

oh sorry, i will change the code

0

string.sub(q.Name,1,1) == "SP" that will always return false because one character is not equal to 2

0

i found the error, the parts where on the same pos, but it was misaligned by 0.00005 studs