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

My level system doesn't work with my weapon, but it works with any other, why?

Asked by 1 year ago

Hi, I wanted to make a level system for my upcoming zombie game, and somewhy my damage script doesn't work with it, I think the damage script is the problem here. And as I tested it, every other weapon work with it. But I don't know how to fix it.

Bullet damage script

local bullet = script.Parent

local damaged = {}

local function onTouch(partOther)

    local humanOther = partOther.Parent:FindFirstChild("Humanoid")

    if not humanOther then return end
    if humanOther.Parent == bullet then return end
    if game.Players:GetPlayerFromCharacter(partOther.Parent) then return end

    if table.find(damaged, humanOther) then return end

    table.insert(damaged, humanOther)

    humanOther:TakeDamage(25)

end

script.Parent.Touched:Connect(onTouch)

Exp give, when zombie dies script:

local Humanoid = script.Parent.Humanoid
function Dead()
    local tag = Humanoid:FindFirstChild("creator")
    if tag ~= nil then
        local leaderstats = tag.Value:FindFirstChild("leaderstats")
        if leaderstats ~= nil then
            leaderstats.Exp.Value = leaderstats.Exp.Value + 10
            wait(0.1)
            script:Remove()
        end
    end
end

Humanoid.Died:Connect(Dead)

And the leaderstat script, if it helps to help me:

game.Players.PlayerAdded:Connect(function(plr)
    local stats = Instance.new("Folder",plr)
    stats.Name = "leaderstats"

    local leavels = Instance.new("IntValue",stats)
    leavels.Name = "Levels"
    leavels.Value = 0
    local exp = Instance.new("IntValue",stats)
    exp.Name = "Exp"
    exp.Value = 0
end)
game.ReplicatedStorage.AddExpEvent.OnServerEvent:Connect(function(plr)
    plr.leaderstats.Exp.Value = plr.leaderstats.Exp.Value + 10
end)

game.Players.PlayerAdded:Connect(function(plr)
    wait(.1)
    local exp = plr.leaderstats.Exp
    local levels = plr.leaderstats.Levels

    while wait() do
        if exp.Value >= (100 * (levels.Value + 1)) then
            levels.Value = levels.Value + 1
            exp.Value = 0
            game.ReplicatedStorage.LevlUpGui:FireClient(plr)

        end
    end
end)


1 answer

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

I couldn't find an issue with your code, are you getting any errors?

You don't need to read this all, but it helps me understand it.

What your script does:

When the bullet object touches another object in the game, the onTouch() function is called. The function first checks if the touched object has a Humanoid child, which indicates that it is a character in the game.

If the touched object is a character, the function checks if the parent of the bullet is the same as the parent of the character. If this is the case, then the function returns early and does not apply damage. This is likely to prevent the bullet from damaging the character that fired it.

Next, the function calls the game.Players:GetPlayerFromCharacter() method to get the Player instance associated with the touched character. If the method returns nil, then the function returns early and does not apply damage. This is likely to prevent the bullet from damaging non-player characters in the game.

Finally, the function checks if the damaged table contains the Humanoid instance of the touched character. If it does, then the function returns early and does not apply damage. This is likely to prevent the bullet from damaging the same character multiple times.

If the function passes all these checks, it applies 25 points of damage to the touched character by calling the humanOther:TakeDamage() method.

Bullet Damage Script:

I added a check to ensure that game.Players:GetPlayerFromCharacter() returns a Player instance before using it. If it returns nil, then the script will return early and not apply damage to the character.

local bullet = script.Parent

local damaged = {}

local function onTouch(partOther)

    local humanOther = partOther.Parent:FindFirstChild("Humanoid")

    if not humanOther then return end
    if humanOther.Parent == bullet then return end

    local player = game.Players:GetPlayerFromCharacter(partOther.Parent)
    if not player then return end

    if table.find(damaged, humanOther) then return end

    table.insert(damaged, humanOther)

    humanOther:TakeDamage(25)

end

script.Parent.Touched:Connect(onTouch)

Exp give, when zombie dies script:

local humanoid = script.Parent.Humanoid

function onDied()
    local creatorTag = humanoid:FindFirstChild("creator")
    if creatorTag == nil then return end

    local leaderstats = creatorTag.Value:FindFirstChild("leaderstats")
    if leaderstats == nil then return end

    leaderstats.Exp.Value = leaderstats.Exp.Value + 10
    wait(0.1)
    script:Remove()
end

humanoid.Died:Connect(onDied)

In this code, I made a few changes:

  1. I renamed the Humanoid variable to humanoid to follow the convention of using lowercase letters for variable names.

  2. I replaced the Dead() function with the onDied() function, which is a more descriptive name.

  3. I added checks to ensure that humanoid:FindFirstChild() and creatorTag.Value:FindFirstChild() return non-nil values before using them. This will prevent errors if the creator tag or leaderstats object are not found.

I hope this helps! Let me know if you have any other questions.

And the leaderstat script, if it helps to help me:

game.Players.PlayerAdded:Connect(function(player)
    local stats = Instance.new("Folder", player)
    stats.Name = "leaderstats"

    local levels = Instance.new("IntValue", stats)
    levels.Name = "Levels"
    levels.Value = 0

    local exp = Instance.new("IntValue", stats)
    exp.Name = "Exp"
    exp.Value = 0
end)

game.ReplicatedStorage.AddExpEvent.OnServerEvent:Connect(function(player)
    player.leaderstats.Exp.Value = player.leaderstats.Exp.Value + 10
end)

game.Players.PlayerAdded:Connect(function(player)
    wait(0.1)

    local exp = player.leaderstats.Exp
    local levels = player.leaderstats.Levels

    while wait() do
        if exp.Value >= (100 * (levels.Value + 1)) then
            levels.Value = levels.Value + 1
            exp.Value = 0
            game.ReplicatedStorage.LevlUpGui:FireClient(player)
        end
    end
end)

In this code, I made a few changes:

  1. I renamed the plr variable to player to follow the convention of using lowercase letters for variable names.

  2. I added checks to ensure that the AddExpEvent object and LevlUpGui object exist in game.ReplicatedStorage before using them. This will prevent errors if these objects are not found.

I hope this helps! Let me know if you have any other questions.

0
Thanks for your reply, but it still does the same and now, the bullet script, doesn't work, and it doesn't gave me any errors, how can I fix that? Kisferenc 47 — 1y
Ad

Answer this question