Hello, I’m making an Inventory system in my game. But I’ve run into an issue with duplicates, it clones them fine but the rest of my script ceases to work as I wrote it. Like just one cloned instance would exist at a time.
The script clones the object but the changing of the text label text doesn’t apply to any instances after the first, which disables a sell button as it’s tied into a set table of text, which one of the entries appears on the text label to determine sell price.
I’ve not tried any solutions so far, as all the solutions I could think up in practice wouldn’t work. I also saw no similar posts anywhere.
Script
local player = script.Parent.Parent.Parent.Parent.Parent.Parent
local RP = game:GetService("ReplicatedStorage")
local inventory = script.Parent.Parent.Parent.Parent.Parent.InventoryGUI.MainFrame.ScrollingFrame
local SOP = RP.SOP
local PButton = script.Parent.Parent.Parent.Parent.Parent.PurchaseGUI.Open
script.Parent.MouseButton1Click:Connect(function()
local quality = math.random(1,5)
local clonedSOP = SOP:Clone()
local itemquality
if quality == 1 then
itemquality = "FN"
end
if quality == 2 then
itemquality = "MW"
end
if quality == 3 then
itemquality = "FT"
end
if quality == 4 then
itemquality = "WW"
end
if quality == 5 then
itemquality = "BS"
end
clonedSOP.Parent = inventory
inventory.SOP.TextLabel.Text = itemquality
script.Parent.Parent.Visible = false
script.Parent.Parent.Parent.Visible = false
PButton.Visible = true
end)
This is held under a button for functional purposes and is a Server Script.
All Help is Appreciated! Thanks in advance!
Further Explanation of my inventory system:
I place the item’s Button in ReplicatedStorage, they get cloned into an inventory frame upon the press of a button which also sets a “Quality” the item’s button causes a dropdown which displays options such as close, trade, sell. The sell button does not work with duplicates because the script isn’t trying to write the text to the text label in the duplicate that the sell button requires. It instead just writes the text to the first clone breaking the second clone.
I’m sure theres a simple solution and I’m overthinking this sorry if I end up wasting your time.
I don’t really get what this is doing honestly. If I’m not getting it wrong, you are controlling a GUI with a server script which is a very bad idea. For the rest, you’re talking about a sell/trade/… buttons and a dropdown that are not working, but you’re not showing us the code for that, so it’s gonna be hard to help.
I’ll try to explain better, I’m using server scripts as all the currency is held server sided so using a local script with anything that deals with the currency won’t update the servers logic on the currency. This makes the currency system bug out.
All I really need done is a way to append either a number or a different character to the end of the cloned objects so they can be differentiated by other scripts. I know that I could use math.random and append a random number to the end of each item, but there’s the risk of duplicates when using math.random.
local original = Instant.new("...")
Instant.new("IntValue", original)
local function Clone(instance)
local clone = instance:Clone()
clone.IntValue.Value += 1
return clone
end
local clone1 = Clone(original)
local clone2 = Clone(clone1)
local clone3 = Clone(clone2)
print("original", original.IntValue.Value)
print("clone1", clone1.IntValue.Value)
print("clone2", clone2.IntValue.Value)
print("clone3", clone3.IntValue.Value)
or
local original = Instant.new("...")
local function Clone(instance)
local clone = instance:Clone()
clone:SetAttribute("Clone", true)
return clone
end
local clone1 = Clone(original)
local clone2 = Clone(clone1)
local clone3 = Clone(clone2)
print("original", original:GetAttribute("Clone"))
print("clone1", clone1:GetAttribute("Clone"))
print("clone2", clone2:GetAttribute("Clone"))
print("clone3", clone3:GetAttribute("Clone"))
Thanks for the great idea on how to do this, I didn’t think of setting an int value that we raise with each clone and append to the end of the clones name!
I think you’re doing a big misconception. It’s normal that your data is managed on the server side. However, it doesn’t mean that your GUI, even though it needs access to this server-sided data, must be controlled by the server. And in fact, it shouldn’t.
Let me give you an example. Let’s take an application like Discord for instance. It’s a web page, comparable to your GUI, except it’s made of HTML code. Do you think everytime some data changes on the server side, the server sends a new piece of HTML code to the client to update the page? The answer is hopefully no. It only sends the data (that has changed) and lets the client sided scripting update the page. And this is even truer on Roblox than on the web, since Roblox GUIs almost systematically require two ways syncing.
Another proof is that you can’t control every aspect of GUIs from the server, as it’s not supposed to work like that.
So how are you supposed to do it then? Here’s the networking pattern that should generally be followed:
Displaying data on the client: retrieve the data from the server and display it
Responding to changes that happen on the server side: the server will broadcast changes to the client, the client has to listen for these changes and appropriately update the display
Making changes to server-sided data: the client will send a request to the server describing what are the desired changes and the server will eventually make changes, which will be broadcasted to the client
This may sound difficult but there are so many ways to implement data replication, many of which are accessible to beginners. Since you are trying to make an inventory, all you have to do is listen to updates the player Backpack's contents and update your UI based on what happens here. To forward queries to the server, use RemoteEvents. You will find dozens of examples of this on the forum and on the web.
Thanks for the reply, most of my scripts within GUI are local script thankfully. Only scripts which add or remove cash value or their functionallity is tied into another script which manages cash value uses a server script. I might implement data VIA remote events in the future, but right now I’m just going to use my current method as it’s keeping things simple for now. I’m also not using the backpack at all the whole inventory system is custom and since the items don’t need to be equip-able or anything they’re just held as a button inside the inventory’s frame.
Appreciate your reply and the info, remote events are definitely the best way to interact client to server and vice versa. But since I doubt this game will ever see the light of day It’s not necessary at the moment.