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

hit detection script isn't working well . Sometimes the hit registers twice :o?

Asked by 5 years ago

So I made a script which detects if theres a zombie and if there is a zombie it shoots a beam but there is one issue. Sometimes it registers the torso twice therfore it deals double the damage . I can't find a solution , ive tried using debounces but I don't know how to use it of for this situation. I want the beam to be able to hit multiple enemies.

local ray = Ray.new(script.Parent.Position,Vector3.new(0,0,-40))
local LaserBean = script.Parent.Parent
local Damage = 40
local Folder = workspace.Folder
local main = script.Parent
local Debris = game:GetService("Debris")
local TweenService = game:GetService("TweenService")
local plr = game.Players

while wait(3)do
    local hit,position = workspace:FindPartOnRayWithIgnoreList(ray,{script.Parent,Folder,workspace.hi,game.Players})
    if hit then


        local k = Instance.new("Part",Folder)
        k.Name = "Beam"
        k.BrickColor = BrickColor.new("Bright blue")
        k.Material = "Neon"
        k.Anchored = true
        k.CanCollide = false

local Info = TweenInfo.new(1.5)
    local Goal = {}
    Goal.Transparency = 1

    local Tween = TweenService:Create(k , Info , Goal) ----tween the transparency of the beam
    Tween:Play()



        script.Teeth.Transparency = 0
        script.Mouth.Transparency = 1


        local Dist = (workspace.hi.Position - main.Position).Magnitude  ---Subtract the length you want the beam to be by the laser bean itself

        k.Size = Vector3.new(.5 , .5 , Dist)
        k.CFrame = CFrame.new(main.Position , hit.Position) * CFrame.new(0,0, - Dist / 2)
            script.Beam:Play()

        k.Touched:Connect(function(hit)

    local Torso = hit.Parent:FindFirstChild("Zombie")
    if Torso then 
        print(hit.name)
        if hit.Name == "Torso" then
               Torso:TakeDamage(Damage)
            else Torso:TakeDamage(0)  ---- if hit torso deal damage else return
        end
    end

    wait(1.5)
        script.Teeth.Transparency = 1
        script.Mouth.Transparency = 0
        k:Destroy()

        end)
    end
    end


Answer this question