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

I need help with my "Randomize" script since it keeps getting errors after dying. Can you help?

Asked by 4 years ago
Edited 4 years ago

Here is my script:

game.ReplicatedStorage.RandomizeItems.OnServerEvent:Connect(function() local function choose(tab) local n = math.random(1, #tab) local obj = tab[n] return obj end

local Amount = math.random(2,9)

if script.Parent:findFirstChild("Torso") then
    for i=1, Amount do
        local LocalPlr = game.Players:FindFirstChild(script.Parent.Name)
        local Item = choose(game.ReplicatedStorage.BackpackItems:GetChildren())
        wait(1.5)
        Item:Clone()
        Item.Parent = LocalPlr.Backpack
    end
end

end)

It goes in the "StarterCharacterScripts" and is a server script.

It keeps getting this error:

RandomItems:3: bad argument #2 to 'random' (interval is empty)

Can you help?

(I cant find any ways to fix my code on my own or the internet)

2 answers

Log in to vote
0
Answered by 4 years ago

Hey this is what you're looking for.

local rs =  game:GetService("ReplicatedStorage")

function getRandomTool(player)
    for i = 1, math.random(1,9) do
        local item = rs.BackpackItems:GetChildren()[math.random(1, #rs.BackpackItems:GetChildren())]:Clone()
        item.Parent = player.Backpack
    end
end

game.Players.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function()
        getRandomTool(plr)
    end)
end)
0
Also, you didn't need a RemoteEvent for this as I used the PlayerAdded event, which is used in a server script. This way you can do everything in one script :) AspectType 151 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

I'm guessing this is because of local n = math.random(1, #tab). I can't see your variables but I'm almost sure of this. Try changing it to a number instead of a variable.

Answer this question