Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How can I fix these weld problems?

Asked by
AZDev 590 Moderation Voter
8 years ago

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

Log in to vote
0
Answered by
Necrorave 560 Moderation Voter
8 years ago

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!

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. AZDev 590 — 8y
Ad
Log in to vote
2
Answered by 8 years ago

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)
0
This is a simple, more elegant way of doing it. XAXA 1569 — 8y
Log in to vote
0
Answered by 8 years ago

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.

Answer this question