Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by
Psudar 882 Moderation Voter
4 years ago
Edited 4 years 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 78 — 4y
0
Thanks for the feedback. I'll check it out sometime. Psudar 882 — 4y

2 answers

Log in to vote
1
Answered by
sleazel 1287 Moderation Voter
4 years 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
            wait(1) -- adjust if necessary
            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 882 — 4y
Ad
Log in to vote
0
Answered by
Alphexus 498 Moderation Voter
4 years 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 + 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``)

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 Alphexus 498 — 4y
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 882 — 4y
1
oh i understand now. The script gets cloned too. Alphexus 498 — 4y
0
Yes sir. Psudar 882 — 4y

Answer this question