Lucky Block Tool Help

I made a Lucky Block tool. When you have the tool equipped and you click on the screen, it will give you a random tool. I have a folder located in ReplicatedStorage that contains all of the possible tools the Lucky Block can give the player.

The Lucky Block works for the most part. But there’s one problem I’m having. When I click on the screen it destroys the Lucky Block and gives me a random tool from the folder. But instead of putting it in the number one spot in my Backpack, it puts it in the number two spot. Here’s a video if you don’t know what I mean.

robloxapp-20230711-1319096.wmv (2.0 MB)

Anyone have a solution? Here’s my code:

local LuckyBlock = script.Parent
local Tools = game.ReplicatedStorage.LuckyBlockTools:GetChildren()
local Rand = math.random(1, #Tools)

local equipped = false

function onActivated()
	if not equipped then
		return
	end

	Tools[Rand]:Clone().Parent = LuckyBlock.Parent
	LuckyBlock:Destroy()
end

function onEquipped()
	equipped = true
end

function Unequipped()
	equipped = false
end

LuckyBlock.Activated:connect(onActivated)
LuckyBlock.Equipped:connect(onEquipped)
LuckyBlock.Unequipped:Connect(Unequipped)

Before I use the lucky block:

Before

After I use it:

After

basically it puts the new tool in the number two spot not the number one and i want it in the number one spot

instead of destroying the luckyblock after the tool’s parent is set try setting the lucky block’s parent out of the player’s inventory, then give the player the tool, then destroy the lucky block

You should change up the orders so the lucky block gets destroyed first, then you clone the tool and parent it in the backpack.

Also use task.wait() between these two things

fast way
Tools[Rand]:Clone().Parent = LuckyBlock.Parent
task.wait()
LuckyBlock:Destroy()

The same thing still happens. It puts the random tool in #2 position and not #1 position.

You are right though. I should have used task.wait…

1 Like

I think this is the problem:

	Tools[Rand]:Clone().Parent = LuckyBlock.Parent
	LuckyBlock:Destroy()

Lua reads code from top to bottom. So if you swap these 2 lines of code, the Lucky block will be deleted first so the tool wont the pushed to the second slot since tools will get pushed to the 2nd slot if the first slot is already occupied

Nope, now it just deletes the tool completely…

Just realized that when you delete the script, the entire script completely stops. I’m pretty sure this is the solution this time.

local backpack --put this at the start of the script

backpack = LuckyBlock.Parent -- put this at the start of the function

	LuckyBlock:Remove()
	Tools[Rand]:Clone().Parent = backpack -- replace the last 2 lines of the function with these 2 lines

It uses :Remove() instead of :Destroy(). Destroy will, well, destroy the object, and Remove sets the object’s parent to nothing and keeps scripts running.

That fixed it. Thank you for the help!

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