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

How can I fix these weld problems?

My gun script welds the bullet to whatever it hits. I fired my gun and then touched it to see what would happen. When I touched it, the weld remained attached to what it hit but then welded me there. How would I prevent this?

Here is the block of code that deals with the weld.

B.Touched:connect(function(hit)
	local weld = Instance.new("Weld", B)
	weld.Part0 = B
	weld.Part1 = hit				
end)

Thanks ahead of time.

3 answers

✓ Accepted answer

You will have to create a boolean of some sort that checks to see if a bullet has already been welded.

Example:

local welded = false

B.Touched:connect(function(hit)
	if ~welded then --Check to see if it has been welded
		welded = true 
		local weld = Instance.new("Weld", B)
		weld.Part0 = B
		weld.Part1 = hit
	end      
end)

This will basically set welded to true once it welds to something. Which should prevent any further Touched events to weld anything.

Let me know if you have any other questions!

1 comment

0

I found my problem. It isn't changing the weld. It adds a new one everytime it is touched. This would work had this actually been the case so I will accept the answer.

You could disconnect the event when the bullet welds once, like so:

local conn
conn = B.Touched:connect(function(hit)
    local weld = Instance.new("Weld", B)
    weld.Part0 = B
    weld.Part1 = hit
    conn:disconnect()                
end)

1 comment

0

This is a simple, more elegant way of doing it.

You need a debounce to disable the script from functioning again. So to start you would go:

debounce = false -- You can change debounce to w/e you want. This is to tell the script that nothing has been hit yet.
B.Touched:connect(function(hit)
if debounce == false then -- Checks to see if debounce is still false, making sure nothing has been hit.
debounce = true -- Sets debounce to true, this will prevent the script from creating more welds.
    local weld = Instance.new("Weld", B)
    weld.Part0 = B
    weld.Part1 = hit  
end             
end)

That's pretty much it, but I suggest you add the weld and/or the bullet to Debris so it removes after a certain amount of time.

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