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

I'm having trouble with an error can someone help?.

Asked by
Damo999 182
8 years ago

This is the error: 10:10:00.631 - Workspace.Part.Script:21: attempt to get length of local 'weapons' (a userdata value)

I'm making this script for the giggles, but it's supposed to give the player a random weapon once the brick stops spinning and lands on the color red. But it keeps giving me an error please help

randomWeapons = {
game.Lighting.Sword,
game.Lighting.Flamethrower
}
spinningPart = script.Parent
spinningTime = 0.001
clickdetector = Instance.new("ClickDetector",spinningPart)
spinning = false


spinningPart.ClickDetector.MouseClick:connect(function(plr)
    if not spinning then
        spinning = true 
        while true do 
            wait(spinningTime)
            spinningPart.CFrame = spinningPart.CFrame *CFrame.Angles(1,0,0)
            spinningPart.BrickColor = BrickColor.Random()
            if spinningPart.BrickColor == BrickColor.Red() then
                print("Giving random weapon")
                for _,weapons in pairs(randomWeapons) do
                    for i = 1,#weapons do
                        local weaponChosen = weapons(math.random(1,#weapons))
                        if weaponChosen then
                            weaponChosen:Clone()
                            weaponChosen.Parent = plr.Backpack
                        end

                    end
                end
                spinning = false
            break
        end
    end
end
end)




1 answer

Log in to vote
1
Answered by
Marios2 360 Moderation Voter
8 years ago

The reason it errors at that line is because you are trying to get the length of a table on a non-table variable.

When pairs or ipairs is performed on a for loop, the script loops as many times as the table length, in descending order (first item given is used first), and the second argument you provide before in is the variable used to refer to the item it is on.

Here's an example of a pairs loop:

local players = game.Players:GetPlayers()
local weapons = {game.ServerStorage.Sword, game.ServerStorage.Slingshot}

for _,player in pairs(players) do
    weapons[math.random(1,#weapons)]:Clone().Parent = player.Backpack --Chooses a weapon and gives a copy to the player's backpack
end

Now, you may have noticed weapons[math.random(1,#weapons)] above. You can refer to a specific item in a table like that. For example, if i input print(weapons[1]), i would get game.ServerStorage.Sword, since that is the first item in the table. Just like that print(weapons[2]) would give off game.ServerStorage.Slingshot.

Therefore,

weapons[1]:Clone().Parent = player.Backpack

would be equal to

game.ServerStorage.Sword:Clone().Parent = player.Backpack

And now your fixed script, using what i just taught you:

randomWeapons = {
game.Lighting.Sword,
game.Lighting.Flamethrower
}
spinningPart = script.Parent
spinningTime = 0.001
clickdetector = Instance.new("ClickDetector",spinningPart)
spinning = false


spinningPart.ClickDetector.MouseClick:connect(function(plr)
    if not spinning then
        spinning = true 
        while true do 
            wait(spinningTime)
            spinningPart.CFrame = spinningPart.CFrame *CFrame.Angles(1,0,0)
            spinningPart.BrickColor = BrickColor.Random()
            if spinningPart.BrickColor == BrickColor.Red() then
                print("Giving random weapon")
                local weaponChosen = randomWeapons[math.random(1,#randomWeapons)] --You don't need the loop after all.
                spinning = false
            break
        end
    end
end
end)
Ad

Answer this question