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

Why does it say Attempt to index nil with kills?

Asked by
Xyternal 247 Moderation Voter
3 years ago

So I wanted to make it, that if someone clicks the click detector, then it checks to see if the part touched the humanoid and killed it. But for some reason, it says attepmt to index nil with kills.

Here are the scripts.

Leaderboard.

game.Players.PlayerAdded:Connect(function(plr)

    local stats = Instance.new("Folder", plr)
    stats.Name = "leaderstats"

    local kills = Instance.new("IntValue", stats)
    kills.Name = "Kills"

end)

And the cannon script i modified to my needs(check the first if statement)

ball = script.Parent
damage = 50

function onTouched(hit)


    local humanoid = hit.Parent:findFirstChild("Humanoid")
    if humanoid~=nil then
        tagHumanoid(humanoid)
        humanoid.Health = humanoid.Health - damage
        if humanoid.Health == 0 or humanoid.Health < 0 then
            local z = game.ReplicatedStorage.Value.Value
            z.leaderstats.Kills.Value += 1
        end
        wait(2)
        untagHumanoid(humanoid)
        connection:disconnect()
    else
        damage = damage / 2
        if damage < 2 then
            connection:disconnect()
            ball.Parent = nil
        end
    end

    if math.random(1,3) == 2 then
        explosion = Instance.new("Explosion")
        explosion.BlastRadius = 9
        explosion.BlastPressure = 1000000 -- these are really wussy units
        explosion.Position = script.Parent.Position
        explosion.Parent = game.Workspace
        connection:disconnect()
        ball.Parent = nil
    end

end

function tagHumanoid(humanoid)
    -- todo: make tag expire
    local tag = ball:findFirstChild("creator")
    if tag ~= nil then
        local new_tag = tag:clone()
        new_tag.Parent = humanoid
    end
end


function untagHumanoid(humanoid)
    if humanoid ~= nil then
        local tag = humanoid:findFirstChild("creator")
        if tag ~= nil then
            tag.Parent = nil
        end
    end
end

connection = ball.Touched:connect(onTouched)

r = game:service("RunService")
t, s = r.Stepped:wait()
d = t + 5.0 - s
while t < d do
    t = r.Stepped:wait()
end

ball.Parent = nil

All help appreciated

2 answers

Log in to vote
0
Answered by
Xyternal 247 Moderation Voter
3 years ago
local z = game.Players:FindFirstChild(game.ReplicatedStorage.Value.Value))

Answering behalf of greatneil

0
lmao greatneil80 2647 — 3y
0
lol Xyternal 247 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Replace your canon script with this

ball = script.Parent
damage = 50

function onTouched(hit)


    local humanoid = hit.Parent:findFirstChild("Humanoid")
    if humanoid~=nil then
        tagHumanoid(humanoid)
        humanoid.Health = humanoid.Health - damage
        if humanoid.Health == 0 or humanoid.Health < 0 then
        local player = game.Players:FindFirstChild(hit.Parent.Name)
            local z = player.leaderstats
            z.Kills.Value += 1
        end
        wait(2)
        untagHumanoid(humanoid)
        connection:disconnect()
    else
        damage = damage / 2
        if damage < 2 then
            connection:disconnect()
            ball.Parent = nil
        end
    end

    if math.random(1,3) == 2 then
        explosion = Instance.new("Explosion")
        explosion.BlastRadius = 9
        explosion.BlastPressure = 1000000 -- these are really wussy units
        explosion.Position = script.Parent.Position
        explosion.Parent = game.Workspace
        connection:disconnect()
        ball.Parent = nil
    end

end

function tagHumanoid(humanoid)
    -- todo: make tag expire
    local tag = ball:findFirstChild("creator")
    if tag ~= nil then
        local new_tag = tag:clone()
        new_tag.Parent = humanoid
    end
end


function untagHumanoid(humanoid)
    if humanoid ~= nil then
        local tag = humanoid:findFirstChild("creator")
        if tag ~= nil then
            tag.Parent = nil
        end
    end
end

connection = ball.Touched:connect(onTouched)

r = game:service("RunService")
t, s = r.Stepped:wait()
d = t + 5.0 - s
while t < d do
    t = r.Stepped:wait()
end

ball.Parent = nil
0
are you sure thats it? Remember, I want the player who clicked the click detector to get a point Xyternal 247 — 3y

Answer this question