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.
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!
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)
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.