Scripting is kind of hard for beginners, so I made this “Random Item Selector” script while I was bored. Let me go step by step on how this script works:
local tools = game:GetService("ReplicatedStorage"):WaitForChild("Tools") -- change this variable to the folder/model with all of the tools.
local numberValue = 1
local tableTools = {}
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
for i, v in pairs(tools:GetChildren()) do -- Starts off with a normal table.
table.insert(tableTools, numberValue, v.Name)
numberValue = numberValue + 1
end
numberValue = 1
character:WaitForChild("Humanoid").Died:Connect(function()
local random = math.random(1, #tools:GetChildren())
wait(8)
repeat wait() -- Removes everything but the selected item itself.
for i, v in pairs(tools:GetChildren()) do
if i ~= random then
table.remove(tableTools, i)
end
end
until next(tableTools)
local clone = tools:WaitForChild(tableTools[1]):Clone()
clone.Parent = player.Backpack
repeat wait() -- removes everything even the selected item.
for i, v in pairs(tools:GetChildren()) do
table.remove(tableTools, i)
end
until next(tableTools)
for i, v in pairs(tools:GetChildren()) do -- Resets the table back into its original form
table.insert(tableTools, numberValue, v.Name)
numberValue = numberValue + 1
end
numberValue = 1
end)
end)
end)
So lets start off with the first for loop. Instead of creating the loop in the actual script source, I decided to make a for loop just incase you add more items and you probably don’t know or you’re to lazy to add it inside the table.
for i, v in pairs(tools:GetChildren()) do -- Starts off with a normal table.
table.insert(tableTools, numberValue, v.Name)
numberValue = numberValue + 1
end
numberValue = 1
In my opinion, this was the hardest part to script. First I had to generate a random number using roblox’s math.random system. Then I removed every item but the selected item from the table.
local random = math.random(1, #tools:GetChildren())
wait(8)
repeat wait() -- Removes everything but the selected item itself.
for i, v in pairs(tools:GetChildren()) do
if i ~= random then
table.remove(tableTools, i)
end
end
until next(tableTools)
Then, I had to put the selected item in the player’s backpack and refreshed the whole table.
local clone = tools:WaitForChild(tableTools[1]):Clone()
clone.Parent = player.Backpack
repeat wait() -- removes everything even the selected item.
for i, v in pairs(tools:GetChildren()) do
table.remove(tableTools, i)
end
until next(tableTools)
for i, v in pairs(tools:GetChildren()) do -- Resets the table back into its original form
table.insert(tableTools, numberValue, v.Name)
numberValue = numberValue + 1
end
numberValue = 1
In conclusion, I hoped this help. If there are any bugs please message me back and I will attempt to fix them. Toodaloo!