Still have questions? Join our Discord server and get real time help.
0

# [Solved] How do I get touchEnded to fire at the right time?

Psudar 131
1 month ago
Edited 1 month ago

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:

0
Touch ended is not always reliable. You shoul use raycast or region dinozaver_triceratop 13 — 1mo
0
Thanks for the feedback. I'll check it out sometime. Psudar 131 — 1mo

1
sleazel 394
1 month ago

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
local magnitude = (redJoin.Position - hit.Parent.HumanoidRootPart.Position).magnitude
until magnitude > 2 --adjust if necessary
redJoin:Destroy()
end

end)
0
Thanks a lot man. Seems like a viable method for what im going w/, I'll definitely implement some magnitude checks. Appreciated. Psudar 131 — 1mo
0
DevvAlpha 136
1 month ago

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 + 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 workspace:FindFirstChild("redNewJoin")then

workspace.redNewJoin:Destroy() --If step off the part, destroy

cloned = false end

end

end)

0
sorry when I was pasting you code to edit it. Some things messed up and it was a hassle. Hope you understand the code and it should work. :D DevvAlpha 136 — 1mo
0
Unfortunately, I do delete the clone. That's why I renamed it. I have the script check for the name of the clone, if the clones name is "redNewJoin" then it deletes. Since the entire part gets cloned, so does the script inside of it, thus making the name belonging to the new script.Parent. Sorry if thats confusing to you. Psudar 131 — 1mo
1
oh i understand now. The script gets cloned too. DevvAlpha 136 — 29d
0
Yes sir. Psudar 131 — 17d