Lucky block script?

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)

sript is not ready and It does not works yet

Would you not just clone the random item and set the parent of the item/tool to the player backpack?

How can I create that in this script?

i think this is what you want

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)
2 Likes

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
1 Like

I know that It is basic stuff but It does not work and does not give me any error which is why I posted

Unfortunately, despite my needs, it is not functioning.

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.

1 Like

Yes, I know that It should do something but does not do anything, I think It is not the script problem anymore

What type of script is it and can you show the layout of the tool.

Oh, I found the bug It was another script that was blocking it. Thanks

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.