Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make this script a little more cleaner? *Edited*

So what I'm trying to do is make it so if two blocks touch when I run the game, they weld together I successfully got it however the welds aren't consistent, such as one side isn't welded to another. Also, every time they touch a non block they spit out error and it's a total mess. Here's what I have so far:

local block = 	script.Parent
block.Touched:connect(function(hit)
	if hit.values.isblock == true then
		local newWeld = Instance.new("Weld", script.Parent)
		newWeld.Part0 = block
		newWeld.Part1 = hit
	else 
		print("hit is not a valid block")
	end
end)

So I've read everyone's answers and there are ways that are cleaner however all of the parts don't weld like so: prntscr.com/iwsylp

2 answers

are you sure this code sometimes works?

What is this line? if hit.values.isblock == true then

did you attach some items to the part you want to weld, named values and isblock? why not use

if hit:IsA("Part") then

instead? it will literally weld it to any part it hits though and to part it hits later, even the character.

3 comments

0

if (hit:IsA("Part") or hit:IsA("UnionOperation") or hit:IsA("MeshPart")) and (not (hit.Name == "Baseplate")) then

0

Yeah but I only want blocks that are the same to weld though.

0

maybe something else is breaking your weld, like moving one of the parts? use UgOsMiLy's suggestion to get rid of the error messages and perhaps setting the parent after setting the parts makes a difference.

Edit: I changed == true to ~= nil It should probably work now.

Try this

local block = script.Parent
block.Touched:Connect(function(hit)
	if hit:FindFirstChild("values") and hit.values:FindFirstChild("isabrick") ~= nil then
		local newWeld = Instance.new("Weld")
		newWeld.Part0 = block
		newWeld.Part1 = hit
newWeld.Parent = script.Parent
	else 
		print("hit is not a valid block")
	end
end)

3 comments

0

You need to explain your code.

0

That's a lot cleaner, I thank you, however there's still blocks that aren't welded on some sides.

0

I've edited it on line 3. Does it work now?

This discussion is preserved as it was. New answers, comments, and votes are closed.