#Touched() Event Help

1 messages · Page 1 of 1 (latest)

dire matrix
#

I have no clue what I am doing. I have tried tutorials online and even asking chatgpt to no avail. This is my first time ever coding in Luau and i am trying my very best to do great. here is my code

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local debris = game:GetService("Debris")

local move1Event = ReplicatedStorage:FindFirstChild("Actions"):WaitForChild("M1Event")

move1Event.OnServerEvent:Connect(function(player)
    local char = player.Character
    
    local humanoid = char:WaitForChild("Humanoid")
    local hrp = char:FindFirstChild("HumanoidRootPart")
    local arm = char:FindFirstChild("Right Arm")
    
    humanoid.WalkSpeed = 0
    
    local animFolder = ReplicatedStorage:FindFirstChild("Animation")
    local anim = animFolder:FindFirstChild("Punch")
    local track  = humanoid:FindFirstChild("Animator"):LoadAnimation(anim)
    track:Play()
    
    
    --Visual Effect---\
--[[]]
    local vfx1 = game.Workspace:FindFirstChild("Attack1VFX"):Clone() --Copy of VFX duh
    vfx1.Anchored = true
    vfx1.CanCollide = false
    vfx1.CFrame = arm.CFrame*CFrame.new(0,0,-4)        --Copies position and orientation from arm to VFX
    print("CFrame Copied.")
    vfx1.Parent = workspace
    --]]
    --Welding:
--[[
    local weld = Instance.new("Weld")
    weld.Part0 = arm
    weld.Part1 = vfx1
    weld.Parent = vfx1
    weld.C0 = CFrame.new(0,0,-4)
    
    --]]
    --Visual Effect---/
    debris:AddItem(vfx1,3)
    
    --HitBox---\
    --
    local hitbox = game.Workspace:WaitForChild("Attack1Hitbox")
    hitbox.Transparency = 1
    hitbox.Anchored = true
    hitbox.CanCollide = false
    hitbox.CFrame = hrp.CFrame*CFrame.new(0,0,-4)
    hitbox.Parent = workspace



    --When Hitbox hits something:
    local hitonce = {}
    local amounta = 0
    hitbox.Touched:Connect(function(hit)
        amounta += 1
        local enemychar = hit:FindFirstAncestorOfClass("Model")
        if not enemychar then return end
        print("almost hit", hitonce[1], enemychar)
        
        local ehumanoid = enemychar.FindFirstChildOfClass("Humanoid")
        if not ehumanoid then return end
        
        if enemychar == "C" then 
            print("thats a mee!")
            return 
        end
        
        
        if hitonce[ehumanoid] then 
            print("neega i told you so")
            return 
        end
        print("it fail :(")

        local enemyhrp = enemychar:FindFirstChild("HumanoidRootPart")
        
        hitonce[ehumanoid] = true
        
        ehumanoid:TakeDamage(10)
    end)
    print(amounta, "That much gaddam!")
--]]
    --HitBox---/
    
    --Ending---\
    --debris:AddItem(hitbox)
    
    humanoid.WalkSpeed = 16
    --Ending---/
end)

I have included the entire script as I don't know what could be an issue and what wont be.

So far what happens is the code will run normally until I die, which causes the touched function to run multiple times for each part that it touches. It looks like it might be dependent on how quickly i spammed my move1Event.

flat helm
#

.Touched will check for PARTS of a model rather the actual model

#

meaning it will check each individual limb

dire matrix
#

well i thought the code would take that into account by finding the ancestor model of whatever part .Touched is touching and store that for the debouncing

worthy sky
#

I Noticed a few things one is missing :enemychar.FindFirstChildOfClass("Humanoid")
It Should be
enemychar:FindFirstChildOfClass("Humanoid")

The Creates hitbox part

hitbox.Transparency = 1
hitbox.Anchored = true
hitbox.CanCollide = false
hitbox.CFrame = hrp.CFrame * CFrame.new(0,0,-4)
hitbox.Parent = workspace ```
it reusing this ONE global hitbox in Workspace
This means:
All players have the same hitbox 
Desync
Exploiters can move it.
Should be: Clone() per attack
#
anim = animFolder.Punch
track = humanoid.Animator:LoadAnimation(anim)
track:Play() ```
It should check if Animator exists before Playing
And Possible a small cooldown for not spamable
alpine raptor
#

i need help with my racer click simulator game
instead of the track spawning in front infinitley it spawns to the side of it and i need to fix this issue

dire matrix
#

yeah definitely shouldve peeped the "." I tried using the clone method before and it just wouldn't spawn an object which was annoying but it does seem to be spawning the hitbox now.

and I also added the debris line so it deletes the clones Thanks bruh 👊

alpine raptor
dire matrix
worthy sky
# dire matrix yeah definitely shouldve peeped the "." I tried using the clone method before an...

Well it must have been the small dot, so
What’s annoying you is every time M1Event fires, you’re creating another .Touched connection to the same hitbox. So when you die and your character parts touch it, all those old connections trigger at once (that’s why it gets worse the more you spam).

What to change is

Rather than reusing the one in Workspace, Clone a new hitbox for each attack.

After the attack, Destroy the hitbox or disconnect the connectionSo the listeners don’t stack

When damaging, it should Remember to ignore your own character

Event spam piling up it should be stopped by a small cooldown

It’s mostly fine now, but it’s just missing a Cleanup and hitboxes for each attack. Once that’s done, the issue of multiple touches after death It Should be Alr

dire matrix
noble spireBOT
#

studio** You are now Level 1! **studio

worthy sky
#

Oh Alr, so is Solvedupvote