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

Why won't these weapons go into the players backpack?

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

local classic = game:GetService("ServerStorage"):WaitForChild("ClassicSword"):Clone()

local M24 = game.ServerStorage:WaitForChild("M24"):Clone()

local sword = function()

classic.Parent = player:WaitForChild("Backpack")

print("sword")

end

local sniper = function()

M24.Parent = player.Backpack

print("sniper")

end

local plevents = {sword, sniper}

local chosen = plevents[1]

chosen()

print(chosen())

end) And how would make "chosen" a random item from the table? The output looks like this: sword sword

1 comment

0

Use math.random(1,#plevents). It will choose a number between 1 and the number of items in the table. (In your case, 2)

1 answer

✓ Accepted answer
local plevents = {sword, sniper}
local chosen = plevents[1]

the "plevents" table has two items, sword and sniper. sword is 1, and sniper is 2. so if you do this

print(plevents[1])
print(plevents[2])

the output will be like this :

sword
sniper

But if you do this

local plevents = {sniper, sword}
local chosen = plevents[1]

Then sniper is 1, and sword is 2. so if you do this

print(plevents[1])
print(plevents[2])

The output will be like this

sniper
sword

Now using this code

print(math.random(1,#plevents))

is basically the same as this

print(math.random(1,2))

that is because the table "plevents" has 2 items and the output of the code above is this

1

or this

2

Now, using this code

print(plevents[math.random(1,#plevents)])

the output will be :

Sword

or

Sniper

Remeber plevents[1] is the sword plevents[2] is the sniper and that math.random(1,#plevents) is basically like math.random(1,2) the 2 in math.random(1,2) is the amount of items there is in the table "plevents" using it will either get you 1 or 2 now im gonna simplify it print(plevents[math.random(1,#plevents)]) =print(plevents[math.random(1,2)]) =print(plevents[1]) or print(plevents[2]) =Sword or Sniper Do you get it now? i hope so.

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