Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Custom Inventory dupes Items. Why? [SOLVED]

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

0

I haven't looked at this yet, but have you removed the one from slot 5?

0

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.

0

As you can see in the server script, I delete the item in the old slot and create it in the new slot

1 answer

Just from what I can see, you deleted the old slot before copying it over, so all you need to change is:

    NewSlot.Value = DataTable["OldSlotValue"]
    OldSlot.Value = "0, 0"

I'm not sure if this works because I can't test it

This discussion is preserved as it was. New answers, comments, and votes are closed.