The problem with the "shared inventory" and the objectequiped system (not the one with the inventory)

I took a break from scripting for almost half a year. Don’t beat me up for implementing everything on the client. Also, keep in mind that the two players have money in common, so they are not tied to the player, but to the value inside the replicated store

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ShopFolders = script.Parent
local objectUIsFolder = ReplicatedStorage.ObjectUis
wait(2)

local function cloneFrames()
	for _, folder in (objectUIsFolder:GetChildren()) do
		local scrollingFrameShop = ShopFolders:FindFirstChild(folder.Name)
		for _, frame in (folder:GetChildren()) do
			if frame:IsA("Frame") then

				local clonedFrame = frame:Clone()
				clonedFrame.SelectBTN.SelectBTN.Text = "Buy"
				clonedFrame.Parent = scrollingFrameShop
				
				for i,player in game.Players:GetChildren() do
					clonedFrame.SelectBTN.SelectBTN.MouseButton1Click:Connect(function()
						local RS = game.ReplicatedStorage.RS
						local Cost = clonedFrame.Cost
						
						local CurrectBalance = RS.Value
						local CurrectCost = Cost.Value
						
						if CurrectBalance < CurrectCost then return end
						RS.Value -= CurrectCost
						
						local InventoryClonedFrame = frame:Clone()
						local InventoryFolders = player.PlayerGui.Inventory.Folders
						InventoryClonedFrame.Parent = InventoryFolders:FindFirstChild(folder.Name)
						
						InventoryClonedFrame.SelectBTN.SelectBTN.MouseButton1Click:Connect(function()
							player.Character.Obj.Value = InventoryClonedFrame
							player.Character.Obj.Type.Value = folder.Name
						end)
						
					end)
				end
			end
		end
	end
end

cloneFrames()

I know that there is something wrong with the code due to the fact that I use for i, players (if anything, yes, I am trying to implement a SHARED inventory for players)

However, for some reason, my friend just said that he bought an item and it did not appear in anyone’s inventory, but for me (I created the server) - yes

	InventoryClonedFrame.SelectBTN.SelectBTN.MouseButton1Click:Connect(function()
		player.Character.Obj.Value = InventoryClonedFrame
		player.Character.Obj.Type.Value = folder.Name
	end)

The problem is also similar, Only 1 player can choose something, and the second player has a strange bug - He chose and the Type changed, But the objectValue did not
I also have problems with this line. I made it on the server. It seems like it should be, BUT DAMN it, I do not know how to make this particular line work only for the clicked player (obviously, FireAllClient, I just thought, tell me if I’m wrong)

help… I’ve been working on this code for an hour now

The reason why this is happening, is because the Players:GetPlayers() loop doesn’t update, even then it’s connected to every player in-game at the time, meaning that something like this could happen:
When 1 player clicks on the button, the server will actually think that every player in-game at the time of the loop clicked on it all at once, causing the item to be bought by every player

One option could be to:

  1. Make a Script with the RunContext set to Client, with it’s purpose to just create and parent the frames of each item
  2. Have the button in every frame fire a RemoteEvent/RemoteFunction to the server, with some sort of ID linked to the item when clicked
  3. Have the server detect when a player fired the event, and do the stuff like deducting the balance, etc.
1 Like

I started doing what you say. Why do I need to create a UI on the client? I will need to check the price on the server later (anti-cheat)

You can check if the player has enough currency on the server’s event, or check on both the client and server to reduce network usage

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.