Hello developers, I am currently making a lucky block, so when a player opens a lucky block it will give a player a random item from replicate storage but I cannot manage to add items in the player’s backpack who opened lucky block can anyone help? Thank you!
local Tool = script.Parent
local gears = game.ReplicatedStorage.tools:GetChildren()
local rand = math.random(1,10)
local player = script.Parent.Parent
function onActivated(plr)
if rand == 1 then
elseif rand == 2 then
elseif rand == 3 then
end
Tool:Remove()
end
function onEquipped()
end
Tool.Activated:connect(onActivated)
Tool.Equipped:connect(onEquipped)
local Tool = script.Parent
local gears = game.ReplicatedStorage.tools:GetChildren()
local rand = math.random(1, #gears)
local equipped = false
function onActivated()
if not equipped then
return
end
gears[rand]:Clone().Parent = Tool.Parent
Tool:Destroy()
end
function onEquipped()
equipped = true
end
function Unequipped()
equipped = false
end
Tool.Activated:connect(onActivated)
Tool.Equipped:connect(onEquipped)
Tool.Unequipped:Connect(Unequipped)
Well what you could do is get the children (that you have done already with the variable “gears”) and then select a random tool from it. You would then just set the parent property like normal after cloneing.
I suggest doing some research if you don’t understand how to code because all that you are doing is quite basic stuff.
Some reference code you can implement into your own code:
local Tool = script.Parent
local gears = game.ReplicatedStorage.tools:GetChildren()
local rand = math.random(1,10)
local player = script.Parent.Parent
local RandomItemSelected = gears[rand]
local Clone = RandomItemSelected:Clone()
Clone.Parent = player.Backpack
Um do you get any errors? The code that @BirdieI90 sent should work, you just need to click when the tool is equipped and it will give you a random tool.