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

[Resolved] Tool existence check causes yielding in the script without error?

Asked by
SWX253 2
4 years ago
Edited 4 years ago

I'm working on a script that gives items to the player when an enemy is killed. It also can give items at a chance such as having a 10% chance to get a legendary sword. Here's how I organize the loot table for my mobs.

local _C = {

DropsWeapon = {
        true, {"Blizzard Striker", {1, 1}}, --Format is DoesItDropAnything?, {WeapoName, {Chances of dropping}}
        {"Glaciersteel Cleaver", {3, 10}},

    }

}

return _C

Here is the item drop script.

local MobConfig = require(script.Parent.MobConfig)
local Mob = script.Parent
local Enemy = Mob.Enemy

function DropItem()
    if (Mob == nil) or (Enemy == nil) then return end
    if (not Enemy:FindFirstChild("Player_Tag")) then return end
    local Tag = Enemy:FindFirstChild("Player_Tag")
    if (not Tag) and (not Tag.Value) then return end
    local plr = Tag.Value
    local BP = plr.Backpack
    local SG = plr.StarterGear
    for i, v in pairs(MobConfig.DropsWeapon) do
        print("Loop Started")
        if v ~= true then
        if MobConfig.WeaponDirectory:FindFirstChild(MobConfig.DropsWeapon[i][1]) then
            local DropChance = Random.new():NextInteger(MobConfig.DropsWeapon[i][2][1], MobConfig.DropsWeapon[i][2][2])
            if MobConfig.DropsWeapon[i][2][1] == DropChance then
            print("Drop Chance Check")
            print(MobConfig.DropsWeapon[i][1])
            if SG then
                if not SG:FindFirstChild(MobConfig.DropsWeapon[i][1]) and not BP:FindFirstChild(MobConfig.DropsWeapon[i][1]) then
                print("SG Check")
                local W1 = MobConfig.WeaponDirectory[MobConfig.DropsWeapon[i][1]]:Clone()
                local W2 = MobConfig.WeaponDirectory[MobConfig.DropsWeapon[i][1]]:Clone()
                W1.Parent = SG
                W2.Parent = BP
                print("Items Given")
                        end
                    end
                end
            end
        end
    end
end

if MobConfig.DropsWeapon[1] == true then
    print("Checked for a drop")
    Enemy.Died:Connect(DropItem)
end

The issue is line 22 doesn't seem to check for the weapons' existence. There aren't any errors but I never see "Items Given" in the output.

1 answer

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

The issue was I forgot to add a check for the existence of the player's Backpack on line 21. I should've went with:

if SG and BP then

instead of

if SG then

on line 21.

Ad

Answer this question