Hello devs! So, I wanted to know, how can I swap tools? I tried to make it but apparently it doesn’t work.
ItemModule
--// ServerStorage Variables
local ItemFolder = game.ServerStorage.Items
local ItemModelsFolder = game.ServerStorage.ItemModels
--// SetUpItemsInEvents Variables
local UnlockSound = script.UnlockSound
local ItemClickDetector = script.ItemClickDetector
--// Other Variables
local debounce = false
--// Main
function item:PickUpItem(itemFolder)
for _, Item in pairs(itemFolder:GetChildren()) do
ItemClickDetector:Clone().Parent = Item.Handle.Hitbox
--// Normal picking up
Item.Handle.Hitbox.ItemClickDetector.MouseClick:Connect(function(plr)
if ItemFolder:FindFirstChild(Item.Name) then
local pickedUpItem = ItemFolder:FindFirstChild(Item.Name):Clone()
Item:Destroy()
pickedUpItem.Parent = plr.Backpack
--// Swapping items
for _, bckpckItem in pairs(plr.Backpack:GetChildren()) do
if #plr.Backpack:GetChildren() == 1 then
if bckpckItem:IsA("Tool") then
local swappingItem = ItemModelsFolder:FindFirstChild(bckpckItem.Name)
if swappingItem then
swappingItem:Clone().Parent = itemFolder
swappingItem.Handle.CFrame = pickedUpItem.Handle.CFrame
end
end
end
end
end
end)
end
end
MainScript
local chapter = Round:SetChapter()
chapter.Parent = game.Workspace
if chapter:FindFirstChild("ItemFolder") then
Item:PickUpItem(chapter.ItemFolder)
else
warn("Could not find ItemFolder.")
end
Any help would be really appreciated
1 Like
If the tool is equipped it’s parent is the character.
Did you mean to destroy Item
? That’s destroying the items you have stored in the chapter’s ItemFolder, which you appear to be using as more of a template you clone from.
What you need to know
Not really but swapping the tools in the workspace I’ve a folder named “ItemFolder” in which all the items are stored. Now in the ServerStorage there are 2 more folders, one is named “ItemModelsFolder” and the other is “ItemFolder”
Where are stuff located
ServerStorge
- ItemFolder: Contains tools(aka items)
- ItemModelsFolder: Contains the model of the tool(aka items)
Workspace
- ItemFolder: Contains the models which are the same in the ItemModelsFolder
Have you tried adding a few prints to verify the click event is being triggered, and that swappingItem
isnt nil?
Problem: It doesn’t swap it just adds the items to my inventory
The tool you’re trying to swap out is bckpckItem
, right? You don’t do anything with that after finding the swappingItem
. I think you might have wanted to either destroy that, or parent it back into ItemFolder?
if swappingItem then
swappingItem:Clone().Parent = itemFolder
swappingItem.Handle.CFrame = pickedUpItem.Handle.CFrame
bckpckItem.Parent = itemFolder -- if you meant to put it back
bckpckItem:Destroy() -- or if you meant to remove it entirely
end
1 Like
Sorry for making you wait my studio is a bit laggy.
So, I suppose these were printed out after I clicked the ClickDetector. But still it’s not swapping.
Well you’re not doing anything with the old bckpckItem
like I mentioned in my previous reply. You need to remove it from the player’s backpack by some means.
This gamer has a cool gaming chair.
Thanks to this gamer and @InfinityDesign for helping out! Video Link