I have this bit of code inside of a script parented to a part.
Basically, when the part is touched, I want it to clone. When it's done being touched, I want it to be deleted. I can't seem to get the touchEnded event to fire when the touch is actually over though. Its firing right when the player stands on it, instead of after the player is done touching it.
Any ideas? ```lua
OriginalPart = workspace:WaitForChild("redJoin")
redJoin = script.Parent --A part
Cloned = false --Debounce
--Touched redJoin.Touched:Connect(function(Hit)
if Hit.Parent:FindFirstChild("Humanoid") and not Cloned then Cloned = true local newJoin = redJoin:Clone() --Clone newJoin.Name = "redNewJoin" newJoin.Position = script.Parent.Position + Vector3.new(4, 0, 0) newJoin.Parent = workspace end
end)
--Touch ended redJoin.TouchEnded:Connect(function(Hit)
if Hit.Parent:FindFirstChild("Humanoid") and Cloned then if script.Parent.Name == "redNewJoin" then redJoin:Destroy() --If step off the part, destroy end end
end) ```
Appreciated :v:
As it has been mention before, TouchEnded is not reliable for humanoids. Both Touched and TouchEnded fire gazilions times for humanoids (insert print(hit) )to get the idea. So your part is destroyed soon after first TouchEnded fires (LeftHand for example). What I do is I check periodically distance (magnitude) between part and character after touch is initiated.
OriginalPart = workspace:WaitForChild("redJoin") redJoin = script.Parent --A part Cloned = false --Debounce --Touched redJoin.Touched:Connect(function(Hit) if Hit.Parent:FindFirstChild("Humanoid") and not Cloned then Cloned = true local newJoin = redJoin:Clone() --Clone newJoin.Name = "redNewJoin" newJoin.Position = script.Parent.Position + Vector3.new(4, 0, 0) newJoin.Parent = workspace repeat wait(1) -- adjust if necessary local magnitude = (redJoin.Position - hit.Parent.HumanoidRootPart.Position).magnitude until magnitude > 2 --adjust if necessary redJoin:Destroy() end end)
Your not deleting the clone. You are deleting the part itself. Also if you renamed the clone, the script.Parent's name would not change. You changed the name of the clone. This might be confusing to understand because I did not explain it that well, but I can provide a code that may work:
OriginalPart
=
workspace:WaitForChild(``"redJoin"``)
redJoin
=
script.Parent
--A part
Cloned
=
false
--Debounce
--Touched
redJoin.Touched:Connect(``function``(Hit)
if
Hit.Parent:FindFirstChild(``"Humanoid"``)
and
not
Cloned
then
Cloned
=
true
local
newJoin
=
redJoin:Clone()
--Clone
newJoin.Name
=
"redNewJoin"
newJoin.Position
=
script.Parent.Position + Vector``3.``new(``4``,
0``,
0``)
newJoin.Parent
=
workspace
end
end``)
--Touch ended
redJoin.TouchEnded:Connect(``function``(Hit)
if
Hit.Parent:FindFirstChild(``"Humanoid"``)
and
Cloned
then
if
workspace:FindFirstChild("redNewJoin")
then
workspace.redNewJoin:Destroy()
--If step off the part, destroy
cloned = false
end
end
end``)