Need help on this touched event

so this is a script that creates a part, and welds it to the player when they hit this part. The issue is, for some reason this loops… creating tons of parts?

local HomeTools = game.Workspace:WaitForChild(‘HomeGoalieTools’)

local homeAlreadyTouched = false

HomeTools.Touched:Connect(function(hit)
if hit.Parent:FindFirstChild(“Humanoid”) then
if not homeAlreadyTouched then
wait(.5)
print(“adding part”)
local Torso = hit.Parent.Torso
local weld = Instance.new(“Weld”)
local part2 = Instance.new(“Part”)
part2.Transparency = 1
part2.Size = Vector3.new(4.4, 4.8, 2)
part2.Name = “HomeTool”
weld.Part0 = Torso
weld.Part1 = part2
weld.Parent = hit.Parent
part2.Parent = workspace
wait(.1)
homeAlreadyTouched = true
print(“Setting true”)
HomeTools.TouchEnded:Connect(function()
weld:Destroy()
part2:Destroy()
homeAlreadyTouched = false
end)
end
end
end)

Formatted code:

local HomeTools = game.Workspace:WaitForChild("HomeGoalieTools")
local homeAlreadyTouched = false

HomeTools.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        if not homeAlreadyTouched then
            wait(.5)
            print("adding part")
            local Torso = hit.Parent.Torso
            local weld = Instance.new("Weld")
            local part2 = Instance.new("Part")
            part2.Transparency = 1
            part2.Size = Vector3.new(4.4, 4.8, 2)
            part2.Name = "HomeTool"
            weld.Part0 = Torso
            weld.Part1 = part2
            weld.Parent = hit.Parent
            part2.Parent = workspace
            wait(.1)
            homeAlreadyTouched = true
            print("Setting true")
            HomeTools.TouchEnded:Connect(function()
                weld:Destroy()
                part2:Destroy()
                homeAlreadyTouched = false
            end)
        end
    end
end)

I wouldn’t recommend using a TouchEnded Event in this instance as it can result in some wonky effects, do consider researching what Region3 is, also why did you encase that event inside the Touched? :thinking: You could just reference the local variables outside the functions:

local HomeTools = game.Workspace:WaitForChild("HomeGoalieTools")
local homeAlreadyTouched = false
local Weld
local Part2

HomeTools.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and not homeAlreadyTouched then
        homeAlreadyTouched = true
        wait(.5)
        print("adding part")
        local Torso = hit.Parent.Torso
        Weld = Instance.new("Weld")
        Part2 = Instance.new("Part")
        Part2.Transparency = 1
        Part2.Size = Vector3.new(4.4, 4.8, 2)
        Part2.Name = "HomeTool"
        Weld.Part0 = Torso
        Weld.Part1 = Part2
        Weld.Parent = hit.Parent
        Part2.Parent = workspace
        wait(.1)
        print("Setting true")
    end
end)

HomeTools.TouchEnded:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") and homeAlreadyTouched then
        Weld:Destroy()
        Part2:Destroy()
        wait(5)
        homeAlreadyTouched = false
        print("Setting false")
    end
end)
1 Like

Thanks. It definitely is much better than my original one, but for some reason continues to loop and create the part multiple times. It’ll make it around 5 times, then stop.

I edited the script a bit, could you try it again? That’s a bit strange

Yeah. I’ll try it right now.

lines
4

Yeah. It appears to still be doing it which is odd?

Try using .magnitude. It’s much better if you want to use .TouchEnded since It’s unreliable and unstable.

Also, I suggest you add a debounce in your script to prevent running twice.

Thanks I’ll definitely try that out!

Yeah I was just about to ask, if he considered trying that. That should fix the loop creating.

Yeah there’s a debounce already, I’m just not sure to why it still repeats itself. My best bet is trying what Andro suggested.

Hi @Huntsman35, give this one a try:

local HomeTools = game.Workspace:WaitForChild("HomeGoalieTools")
local Weld
local Part2

function addPartFromHit(hit)
	print("adding part")
	local Torso = hit.Parent.Torso
	Weld = Instance.new("Weld")
	Part2 = Instance.new("Part")
	Part2.Transparency = 1
	Part2.Size = Vector3.new(4.4, 4.8, 2)
	Part2.Name = "HomeTool"
	Weld.Part0 = Torso
	Weld.Part1 = Part2
	Weld.Parent = hit.Parent
	Part2.Parent = workspace
end

function removePart()
	Weld:Destroy()
	Part2:Destroy()
end

local touchEndedConnection = nil
HomeTools.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") and not touchEndedConnection then		
		touchEndedConnection = HomeTools.TouchEnded:Connect(function(hitToDisconnect)
			-- Allow to be touched again 1 second after the body part that first touched HomeTools stops touching it.
			if hit == hitToDisconnect then
				removePart()
				touchEndedConnection:Disconnect()
				wait(1)
				touchEndedConnection = nil
			end
		end)
		addPartFromHit(hit)		
	end
end)

Hello. This sorta works. The only issue is that the part automatically deletes itself like almost instantly when you enter the part. It doesn’t repeat itself though.

You can delay the deletion to be as you like, you can move the removal to after the wait.