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.
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:
After I use it:
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
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
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.