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

why is touched event not working properly????

	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??

3 comments

0

Am I right in saying One Piece? (Yes, it's not an answer to your question. Apologies)

0

yes u right

0

What is the purpose of having the argument "en" and checking for it in touch ended?

1 answer

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.

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