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

Gun script doesn't detect humanoid killed?

Asked by 4 years ago
local Damage = 25
local HeadshotDamage = 50
local Debounce = false

script.Parent.FireEv.OnServerEvent:Connect(function(Player, Shooter, MousePos)
    if script.Parent.Ammo.Value == 0 then return end
    script.Parent.Ammo.Value = script.Parent.Ammo.Value - 1
    script.Parent.FirePart.Fire:Play()
    local Bullet = Instance.new("Part", workspace)
    Bullet.CFrame = script.Parent.FirePart.CFrame
    Bullet.Size = Vector3.new(0.15, 0.15, 0.15)
    Bullet.BrickColor = BrickColor.new("Bright yellow")
    Bullet.TopSurface = "Smooth"
    Bullet.BottomSurface = "Smooth"
    Bullet.Material = "Neon"
    Bullet.CanCollide = false
    local BVol = Instance.new("BodyVelocity", Bullet)
    BVol.P = 5000
    BVol.Velocity = MousePos.lookVector * 250
    local I = 0
    while I < 1000 do
        Bullet.Touched:Connect(function(Toucher)
            if Toucher.Parent == Shooter then return end
            local Hum = Toucher.Parent.Humanoid
            if not Hum then
                I = 1000
                Bullet:Destroy()
            end
            if Toucher.Name == "Head" then
                Hum:TakeDamage(HeadshotDamage)
                print(Hum.Parent.Name)
                Hum.Died:Connect(function()
                    print("Oop, He ded")
                    local OwnerSpyVal = script.Parent.Parent:GetPlayerFromCharacter().Spy.Value
                    print("I wonder if he's the spy...")
                    if OwnerSpyVal == false and Hum.Parent:GetPlayerFromCharacter().Spy.Value == false then
                        print("Oh no he's not!")
                        script.Parent.Parent:GetPlayerFromCharacter():Kick()
                    end
                end)
                I = 1000
                Bullet:Destroy()
            else
                Hum:TakeDamage(Damage)
                I = 1000
                Bullet:Destroy()
            end
        end)
        I = I + 1
        wait()
    end
    Bullet:Destroy()
end)

There are no errors, but when it reaches the point where it checks if a humanoid died, it doesn't print anything after I kill another player.

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

I think it's because the Hum.Died event will only run if it was a headshot because it's under the if statement that checks if it was a headshot. To fix it it should b e something like that:

local Damage = 25
local HeadshotDamage = 50
local Debounce = false

script.Parent.FireEv.OnServerEvent:Connect(function(Player, Shooter, MousePos)
    if script.Parent.Ammo.Value == 0 then return end
    script.Parent.Ammo.Value = script.Parent.Ammo.Value - 1
    script.Parent.FirePart.Fire:Play()
    local Bullet = Instance.new("Part", workspace)
    Bullet.CFrame = script.Parent.FirePart.CFrame
    Bullet.Size = Vector3.new(0.15, 0.15, 0.15)
    Bullet.BrickColor = BrickColor.new("Bright yellow")
    Bullet.TopSurface = "Smooth"
    Bullet.BottomSurface = "Smooth"
    Bullet.Material = "Neon"
    Bullet.CanCollide = false
    local BVol = Instance.new("BodyVelocity", Bullet)
    BVol.P = 5000
    BVol.Velocity = MousePos.lookVector * 250
    local I = 0
    while I < 1000 do
        Bullet.Touched:Connect(function(Toucher)
            if Toucher.Parent == Shooter then return end
            local Hum = Toucher.Parent.Humanoid
            if not Hum then
                I = 1000
                Bullet:Destroy()
            end
            if Toucher.Name == "Head" then
                Hum:TakeDamage(HeadshotDamage)
                print(Hum.Parent.Name)
                I = 1000
                Bullet:Destroy()
            else
                Hum:TakeDamage(Damage)
                I = 1000
                Bullet:Destroy()
            end
        end)
        I = I + 1
        wait()
    end
    Bullet:Destroy()
end)
local humanoid = game.Players.LocalPlayer.Character.Humanoid
humanoid.Died:Connect(function()
  print("Oop, He ded")
                    local OwnerSpyVal = --idk where the spy value is, so write it here
                    print("I wonder if he's the spy...")
                    if OwnerSpyVal == false and Hum.Parent:GetPlayerFromCharacter().Spy.Value == false then
                        print("Oh no he's not!")
                        script.Parent.Parent:GetPlayerFromCharacter():Kick()
                    end
                end)

Now, I'm not sure about it, but it might be the problem. If it still doesn't work, please make a comment in my answer.

0
I added to that, but it didn't seem to fix the issue itchymoonfire 179 — 4y
Ad

Answer this question