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

why is touched event not working properly????

Asked by
tacotown2 119
5 years ago
Edited 5 years ago
    local remote = game.ReplicatedStorage.Remotes.MeraLogia
        remote.OnServerEvent:Connect(function(plr, char)

            local UpperTorso = plr.Character:WaitForChild("UpperTorso")
        local dmg = false

local arm = plr.Character.LeftHand
local LeftFoot = plr.Character.LeftFoot
local RightFoot = plr.Character.RightFoot
local RightHand = plr.Character.RightHand

        UpperTorso.Touched:Connect(function(hit)
                if hit then
                    dmg = true
                    local a = game.ReplicatedStorage.MeraMeraMoves.fef:Clone()
                    a.Name = "a"
                    a.Parent = UpperTorso
                end
        end)

        UpperTorso.TouchEnded:Connect(function(en)
            if en then
             UpperTorso:WaitForChild("a"):Destroy()
            end
        end)
        end)

so if i touch something i want the effect to be in the uppertorso and it works but sometimes i mean a lot of times the effect stays there when it not even touching and when im touching something it wont add the effect sometimes how do i fix this??

0
Am I right in saying One Piece? (Yes, it's not an answer to your question. Apologies) GlennJoakim 35 — 5y
0
yes u right tacotown2 119 — 5y
0
What is the purpose of having the argument "en" and checking for it in touch ended? TheMysticLynxx 89 — 5y

1 answer

Log in to vote
0
Answered by
Trew86 175
5 years ago
Edited 5 years ago

When a touched function works, but not the way you'd expect it to, it probably needs a debounce. Try making the script check if there already is an "a" in UpperTorso, so that an extra "a" isn't created.

  local remote = game.ReplicatedStorage.Remotes.MeraLogia

        remote.OnServerEvent:Connect(function(plr, char)

            local UpperTorso = plr.Character:WaitForChild("UpperTorso")
        local dmg = false

local arm = plr.Character.LeftHand
local LeftFoot = plr.Character.LeftFoot
local RightFoot = plr.Character.RightFoot
local RightHand = plr.Character.RightHand

           UpperTorso.Touched:Connect(function(hit)
                if hit then
        local already = UpperTorso:FindFirstChild("a")
        if not already then
                    dmg = true
                    local a = game.ReplicatedStorage.MeraMeraMoves.fef:Clone()
                    a.Name = "a"
                    a.Parent = UpperTorso
                end
        end
        end)

        UpperTorso.TouchEnded:Connect(function(en)
            if en then
            UpperTorso:WaitForChild("a"):Destroy()
            end
        end)
        end)

What probably happened is that when the player walked into whatever thing, the Touched event fired more than one time for the UpperTorso, creating multiple "a"'s. The touchEnded function only destroys one of the a's, so there are some remaining ones.

Ad

Answer this question