Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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?


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:

2 comments

0

Touch ended is not always reliable. You shoul use raycast or region

0

Thanks for the feedback. I'll check it out sometime.

2 answers

✓ Accepted answer

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)

1 comment

0

Thanks a lot man. Seems like a viable method for what im going w/, I'll definitely implement some magnitude checks. Appreciated.

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``)

4 comments

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

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.

1

oh i understand now. The script gets cloned too.

0

Yes sir.

This discussion is preserved as it was. New answers, comments, and votes are closed.