I have created a custom inventory this works very well, but now I wanted to make a system in which the item, for example, on slot 5 is moved to slot 4 if slot 4 is empty.
I've made a script that partially works, however, duplicates the script the item to be moved
Here is the script:
Local Script:
wait ()
while true do
wait (1)
local ID2Replace = 0
local Slots = 0
local ActiveSlot = game.Players.LocalPlayer.PlayerSystem.ActiveSlot.Value
local SaveSlot = game.Players.LocalPlayer.PlayerSystem:FindFirstChild("SaveSlot"..ActiveSlot)
if SaveSlot ~= nil then
local SlotsLength = SaveSlot.Inventory.Bag1:GetChildren()
repeat
Slots = Slots + 1
local CurrentSlot = SaveSlot.Inventory.Bag1:FindFirstChild("SLOT"..Slots)
if CurrentSlot.Value ~= "0, 0" then
local Slot2Test = SaveSlot.Inventory.Bag1:FindFirstChild("SLOT"..Slots - 1)
if Slot2Test ~= nil then
if Slot2Test.Value == "0, 0" and Slot2Test ~= nil then
Slot2Replace = Slots
local DataTable = {
["OldSlot"] = CurrentSlot.Name,
["NewSlot"] = Slot2Test.Name,
["OldSlotValue"] = CurrentSlot.Value
}
game.Workspace.System.Events.ReplaceEvent:FireServer(DataTable)
end
end
end
until Slot2Replace ~= 0 or Slots == #SlotsLength
end
end
ServerScript:
script.Parent.Parent.Events.ReplaceEvent.OnServerEvent:Connect(function (Player, DataTable)
local ActiveSlot = Player.PlayerSystem.ActiveSlot.Value
local SaveSlot = Player.PlayerSystem:FindFirstChild("SaveSlot"..ActiveSlot)
if SaveSlot ~= nil then
local OldSlot = SaveSlot.Inventory.Bag1:FindFirstChild(DataTable["OldSlot"])
local NewSlot = SaveSlot.Inventory.Bag1:FindFirstChild(DataTable["NewSlot"])
if OldSlot ~= nil and NewSlot ~= nil then
OldSlot.Value = "0, 0"
NewSlot.Value = DataTable["OldSlotValue"]
end
end
end)
That solved the problem:
OldSlot.Value = "0, 1"
wait (1)
OldSlot.Value = "0, 0"

3 comments
I haven't looked at this yet, but have you removed the one from slot 5?
Why don't you have it so that it just copies the item in slot 5 to slot 4, then deletes the item in slot 5? Instead of trying to move it, which seems to be causing you problems.
As you can see in the server script, I delete the item in the old slot and create it in the new slot