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

Why does this reward the cash and kill twice instead of once?

Asked by 9 years ago

As you can probably guess from the title, the following script should reward the owner of the knife 1 kill and 15 Cash. Instead, it gives 2 kills and 30 Cash. I cannot find the problem though. Please help?

This is the function I suspect that has the issue in it: This chunk is around 60 lines long

function BlowDamage(hit, damage)
    local humanoid = hit.Parent:FindFirstChild('Humanoid')
    local player = PlayersService:GetPlayerFromCharacter(hit.Parent)
    if humanoid == nil then
        Handle.WallHit:Play()
        wait(0.1)
        a = game.Workspace:FindFirstChild("Handle")
        if a == nil then
        else
            a:Destroy()
        end
    else
        if humanoid.Health == 0 then
            game.Players.LocalPlayer.leaderstats.Kills.Value = game.Players.LocalPlayer.leaderstats.Kills.Value + 1
            game.Players.LocalPlayer.leaderstats.Cash.Value = game.Players.LocalPlayer.leaderstats.Cash.Value + 15
            Handle.DeadPlayer:Play()
            Handle.DeadSound:Play()
        else
            humanoid.Health = humanoid.Health - THROWING_DAMAGE
            if humanoid.Health <= 0 then
                game.Players.LocalPlayer.leaderstats.Kills.Value = game.Players.LocalPlayer.leaderstats.Kills.Value + 1
                game.Players.LocalPlayer.leaderstats.Cash.Value = game.Players.LocalPlayer.leaderstats.Cash.Value + 15
                Handle.DeadPlayer:Play()
                Handle.DeadSound:Play()
            end
        end
        wait(0.1)
        if a == nil then
        else
            a:Destroy()
        end
    end
    if humanoid ~= nil and MyHumanoid ~= nil and humanoid ~= MyHumanoid then
        if not MyPlayer.Neutral then        
            -- Ignore teammates hit
            if player and player ~= MyPlayer then
                return
            end
        end
        -- final check, make sure weapon is in-hand
        local rightArm = MyCharacter:FindFirstChild('Right Arm')
        if (rightArm ~= nil) then
            -- Check if the weld exists between the hand and the weapon
            local joint = rightArm:FindFirstChild('RightGrip')
            if (joint ~= nil and (joint.Part0 == Handle or joint.Part1 == Handle)) then
                -- Make sure you only hit them once per swing
                if player and not HitPlayers[player] then
                    TagHumanoid(humanoid, MyPlayer)
                    print("Sending " .. damage)
                    humanoid:TakeDamage(damage)
                    Handle.Splat.Volume = 1
                    Handle.Splat:Play()
                    HitPlayers[player] = true
                end
            end
        end
    end
end

If that's not the problem, here's the link to the full knife script: WARNING!! ITS AROUND 300 LINES LONG Knife Script

1 answer

Log in to vote
0
Answered by 9 years ago

The problem is that the weapon is coming into contact with multiple parts of the player.

It looks like you added lines 4-32 to a script that already had the rest of it defined. ex, it looks like you're having the humanoid take damage twice: line 50 has a :TakeDamage() call, and line 19 has a health decrease (TakeDamage is preferred since it will respect forcefields). You should merge the 'if' statements. Note that there's already a "debounce" feature (which fixes your problem) used on line 47, if player and not HitPlayers[player] then -- if a player has been hit with the weapon, lines 48 - 53 won't get run again. You should have your "if humanoid is dead" code there.

[EDIT] "How do I merge 2 ifs together... And which lines do you recommend to be removed?" On line 4, you have if humanoid == nil then. You can combine the code you have there with the if block that starts on line 33, if humanoid ~= nil and MyHumanoid ~= nil and humanoid ~= MyHumanoid then. In other words, you need to move some code around:

function BlowDamage(hit, damage)
    local humanoid = hit.Parent:FindFirstChild('Humanoid')
    local player = PlayersService:GetPlayerFromCharacter(hit.Parent)
    if humanoid == nil then
        BLOCK1
    else
        BLOCK2
    end

--Delete the above "if humanoid == nil" if block and move the code blocks (marked by BLOCK1 and BLOCK2) to the below section:

    if humanoid ~= nil and MyHumanoid ~= nil and humanoid ~= MyHumanoid then
        if not MyPlayer.Neutral then        
            -- Ignore teammates hit
            if player and player ~= MyPlayer then
                return
            end
        end
        -- final check, make sure weapon is in-hand
        local rightArm = MyCharacter:FindFirstChild('Right Arm')
        if (rightArm ~= nil) then
            -- Check if the weld exists between the hand and the weapon
            local joint = rightArm:FindFirstChild('RightGrip')
            if (joint ~= nil and (joint.Part0 == Handle or joint.Part1 == Handle)) then
                -- Make sure you only hit them once per swing
                if player and not HitPlayers[player] then
                    TagHumanoid(humanoid, MyPlayer)
                    print("Sending " .. damage)
                    humanoid:TakeDamage(damage)
                    Handle.Splat.Volume = 1
                    Handle.Splat:Play()
                    HitPlayers[player] = true
           BLOCK2 --put code block2 here, but take out duplicate lines (note how, in the previous few lines, a sound is played and the humanoid receives damage)
                end
            end
        end
    else --this line is new
    BLOCK1 --put code block1 here
    end
end
0
How do I merge 2 ifs together... And which lines do you recommend to be removed? fahmisack123 385 — 9y
0
I edited it to explain. Does that help? chess123mate 5873 — 9y
0
Very little though. I'm still not 100% sure fahmisack123 385 — 9y
Ad

Answer this question