I am doing a commission thingy for someone and I need this to be done today (kinda), I’m basically nearly finished. I just need to contact them asking if there is anything else I can add into it. Anyways - I am making an Inventory system for someone, and everything is fine except for this one feature, where you can press P to put your equipped tool back into your inventory. It looks, to me, as if I have coded this right but it is not working. I have taken a video and showcased what it is meant to do. Just so you know, nothing appears in the console. Vimeo
Here is the hierarchy of the thing
Inventory gui:
Replicated storage:
Code for the script in ServerScriptService:
local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("InventoryData")
function saveData(p)
local inventory = {}
for i, value in pairs(p.Inventory:GetChildren()) do
inventory[value.Name] = value.Value
end
ds:SetAsync(p.UserId, inventory)
end
game.Players.PlayerAdded:Connect(function(p)
local invFolder = Instance.new("Folder", p)
invFolder.Name = "Inventory"
for i, item in pairs(game.ReplicatedStorage.Items:GetChildren()) do
local newValue = Instance.new("IntValue")
newValue.Name = item.Name
newValue.Value = 0
newValue.Parent = invFolder
end
p.CharacterAdded:Connect(function(c)
c.Humanoid.Touched:Connect(function(hit)
if hit:FindFirstChild("ITEM") then
invFolder[hit.Name].Value += 1
hit:Destroy()
elseif hit.Parent:IsA("Tool") and hit:FindFirstChild("ITEM") then
invFolder[hit.Name].Value += 1
hit:Destroy()
end
end)
end)
local invData = ds:GetAsync(p.UserId) or {}
for itemName, itemCount in pairs(invData) do
invFolder[itemName].Value = itemCount
end
end)
game.ReplicatedStorage.InventoryRE.OnServerEvent:Connect(function(p, instruction, item)
if instruction == "drop" and p.Inventory:FindFirstChild(item) and p.Inventory[item].Value > 0 then
p.Inventory[item].Value -= 1
local newItem = game.ReplicatedStorage.Items[item]:Clone()
newItem.CFrame = p.Character.HumanoidRootPart.CFrame - p.Character.HumanoidRootPart.CFrame.LookVector * 5
newItem.Parent = workspace
elseif instruction == "equip" and p.Inventory:FindFirstChild(item) and p.Inventory[item].Value > 0 then
p.Inventory[item].Value -= 1
local newItem = game.ReplicatedStorage.Items[item]:Clone()
newItem.Parent = p.Backpack
elseif instruction == "dropTool" and p.Inventory:FindFirstChild(item) and p.Inventory[item].Value > 0 then
p.Inventory[item].Value -= 1
local newItem = game.ReplicatedStorage.Items[item]:Clone()
local newItem2 = newItem.Handle
newItem2.Parent = workspace
for i,v in pairs(newItem2:GetChildren()) do
v:Destroy()
end
script.ITEM:Clone().Parent = newItem2
newItem:Destroy()
newItem2.CFrame = p.Character.HumanoidRootPart.CFrame - p.Character.HumanoidRootPart.CFrame.LookVector * 5
newItem2.Name = game.ReplicatedStorage.Items[item].Name
end
end)
game.ReplicatedStorage.InventoryRE2.OnServerEvent:Connect(function(p, instruction, item)
if instruction == "putBackTool" and p.Inventory:FindFirstChild(item) and p.Inventory[item].Value < 1 then
local invFolder = p:FindFirstChild("Inventory")
invFolder[item].Value += 1
end
end)
game.Players.PlayerRemoving:Connect(saveData)
game:BindToClose(function()
for i, p in pairs(game.Players:GetPlayers()) do
saveData(p)
end
end)
Code for the script in the gui:
local button = script.Parent
button.Position = UDim2.new(0.032, 0, 0.5, 0)
local open = false
button.MouseButton1Click:Connect(function()
open = not open
local newPos = open and UDim2.new(0.319, 0, 0.5, 0) or UDim2.new(0.032, 0, 0.5, 0)
button:TweenPosition(newPos, "InOut", "Quint", 0.5)
end)
local inventoryFolder = game.Players.LocalPlayer:WaitForChild("Inventory")
function updateInv()
for i, child in pairs(script.Parent.InventoryScroller:GetChildren()) do
if child:IsA("TextButton") then child:Destroy() end
end
for i, item in pairs(inventoryFolder:GetChildren()) do
if item.Value > 0 then
local newButton = script.ItemButton:Clone()
newButton.ItemName.Text = item.Name
newButton.Count.Text = item.Value
local cam = Instance.new("Camera", newButton.ItemFrame)
newButton.ItemFrame.CurrentCamera = cam
local displayItem = game.ReplicatedStorage.Items[item.Name]:Clone()
displayItem.Parent = newButton.ItemFrame
if displayItem:IsA("Tool") then
cam.CFrame = CFrame.new(displayItem.Handle.Position + displayItem.Handle.CFrame.LookVector * 4, displayItem.Handle.Position)
else
cam.CFrame = CFrame.new(displayItem.Position + displayItem.CFrame.LookVector * 4, displayItem.Position)
end
newButton.MouseButton1Click:Connect(function()
if displayItem:IsA("Tool") then
game.ReplicatedStorage.InventoryRE:FireServer("equip", item.Name)
else
game.ReplicatedStorage.InventoryRE:FireServer("drop", item.Name)
end
end)
newButton.MouseButton2Click:Connect(function()
if displayItem:IsA("Tool") then
game.ReplicatedStorage.InventoryRE:FireServer("dropTool", item.Name)
end
end)
newButton.Parent = script.Parent.InventoryScroller
end
end
script.Parent.InventoryScroller.CanvasSize = UDim2.new(0, 0, 0, script.Parent.InventoryScroller.UIGridLayout.AbsoluteContentSize.Y)
end
updateInv()
for i, item in pairs(inventoryFolder:GetChildren()) do
item.Changed:Connect(updateInv)
end
Code for the tool client script:
local mouse = game.Players.LocalPlayer:GetMouse()
mouse.Button1Down:Connect(function()
script.Parent.Fire:FireServer(mouse.Hit.p)
end)
mouse.KeyDown:Connect(function(K)
if K == "p" then
game.ReplicatedStorage.InventoryRE2:FireServer("putBackTool", script.Parent.Name)
script.Parent:Destroy()
end
end)