Making an item system where you basically click E to pick something up and it welds it to your player and then you can drop it. However, I want to incorporate tools into this to allow switching between items without having to drop one and pick another up.
Thing is, I don’t really know how to go about this without causing a loop (eg. player interacts with item to run PickUp function which also creates the tool, once the tool is equipped we run PickUp to weld it but then it’ll loop back over and over) or dealing with unequipping since it’s not as simple as deleting a weld, we need to store it in some place until it’s reequipped like default tools do but I don’t know where. Any help is greatly appreciated, thanks!
function ItemModule:PickUp(player)
if table.find(PlayerProfiles[tostring(player.UserId)].Inventory, self) then return end --makes sure its not already in inventory
self:Switch(player) --switch the status to determine whether to drop or pick up the model
if self.Status == true then --pick up the model
local character = player.Character
if not character then return end
if PlayerProfiles[tostring(player.UserId)].CurrentItem ~= false then return end --makes sure you cant pick up 2 objects and have them welded to you at once
self:SetPlayer(player, true)
PlayerProfiles.AddToInventory(player, self.ItemName)
local root = character.HumanoidRootPart
if root then --create the weld
self.Weld = Instance.new("Weld")
self.Weld.Part0 = root
self.Weld.Part1 = self.Model.Primary
self.Weld.C0 = CFrame.new(0, 0, -self.Distance)
self.Weld.Parent = root
end
elseif self.Status == false then -- drop the model
self:SetPlayer(player, false)
PlayerProfiles.RemoveFromInventory(player, self.ItemName)
if self.Weld then --destroy the weld
self.Weld:Destroy()
self.Weld = nil
end
end
end
function ItemModule:SetPlayer(player, value)
if game.Players:FindFirstChild(player.Name) then
self.PlayerOwner = player
local plrProfile = PlayerProfiles.GetProfile(player.Name)
if value == true then
plrProfile.CurrentItem = self
player:SetAttribute("ItemName", self.ItemName)
else
plrProfile.CurrentItem = false
player:SetAttribute("ItemName", "None")
end
end
end
--Switches the status. Eg from on to off, picked up to dropped and vice versa
function ItemModule:Switch(player)
self.Status = not self.Status
-- print(player.Name .. " turned " .. self.ItemName .. " " .. tostring(self.Status) .. "!")
player:SetAttribute("CurrentItem", self.Status)
end