Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

1 comment

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 :)

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.