How to prevent value duplications

Hello ,
I have problems with changing string value . whenever I change the value , every value also change as well. I don’t know how to get out of this problem . Any help is appriciated :slight_smile:

Here is the script:

local player = game.Players.LocalPlayer
local gui = script.Parent
local backpack = player.Backpack
local char = player.Character or player.CharacterAdded:Wait()

gui.MouseButton1Click:Connect(function()
	print("halo how are you")

	if gui.CanDes.Value == false then

		gui.CanDes.Value = true
		gui.BackgroundColor3 = Color3.fromRGB(75,75,75)

		if not backpack:FindFirstChild(gui.Name) or char:FindFirstChild(gui.Name) then

			game.ReplicatedStorage:WaitForChild("Clone"):FireServer(gui.Name)

			print("halo")

			for i , child in pairs(player.PlayerGui:WaitForChild("HotbarGui").Frame:GetChildren()) do
				print("imsn")
				if child:IsA("ImageButton") then
					if child.ItemName.Value == ""  or child.ItemName.Value ~= child.ItemName.Value then
						game.ReplicatedStorage.UpdateVal:FireServer(child , gui)

						print("ok ok ok ok")
						
					else
						print("ok bruh")
						return
					end
				
				end

			end
			print("OK...")

		else
			char.Humanoid:UnequipTools()
			local desitem = backpack:FindFirstChild(gui.ItemName.Value)

			if desitem then
				desitem:Destroy()
			end

		end

	else
		gui.CanDes.Value = false
		gui.BackgroundColor3 = Color3.fromRGB(255,255,255)
	end
end)

Can you give more info? Which value? What do you mean “every value also change as well”?

Also, instead of print("OK...") and stuff, try Debugging | Roblox Creator Documentation to step through your code line-by-line and see what it’s doing.

I mean this:

as you can see on the hotbar that all the image are same wich means that all the value inside it are same

Also I did do some debugging and it print well just what I want it , but it only change all the value instead one.Hope you understand

Its a string value that has a tool name inside it

I suggest putting comments within code because I cannot understand what is going on or what is being changed.

here the script .

local player = game.Players.LocalPlayer
local gui = script.Parent
local backpack = player.Backpack
local char = player.Character or player.CharacterAdded:Wait()

gui.MouseButton1Click:Connect(function()--fire when clicked
	print("halo how are you")

	if gui.CanDes.Value == false then--check Candes(value inside the gui)

		gui.CanDes.Value = true--make it true
		gui.BackgroundColor3 = Color3.fromRGB(75,75,75)--change color

		if not backpack:FindFirstChild(gui.Name) or char:FindFirstChild(gui.Name) then--chech if backpack doesn't have a tool that its name is same as gui

			game.ReplicatedStorage:WaitForChild("Clone"):FireServer(gui.Name)--fire remote event

			print("halo")

			for i , child in pairs(player.PlayerGui:WaitForChild("HotbarGui").Frame:GetChildren()) do--loop throuhg hotbar gui
				print("imsn")
				if child:IsA("ImageButton") then--chech if child is image buttton
					if child.ItemName.Value == ""  then--check if value(inside child) == empty string
						
						game.ReplicatedStorage.UpdateVal:FireServer(child , gui)--fire server
						
					elseif child.ItemName.Value == child.ItemName.Value then--chech if value(inside child) == another value or value(inside child) not = empty string
						 
							
					return--exit loop
						

						print("ok ok ok ok")
						
					else return end--chech if if value(inside child)not = empty string then exit loop
					
				end

			end
			print("OK...")

		else
			

		end

	else
		gui.CanDes.Value = false
		gui.BackgroundColor3 = Color3.fromRGB(255,255,255)--------------------dont mind this part
		game.ReplicatedStorage.DestroyTool:FireServer(gui.Name)
		
	end
end)

hope you enderstand

and why are you needing to fire to the server?

game.ReplicatedStorage:WaitForChild("Clone"):FireServer(gui.Name)--fire remote event

game.ReplicatedStorage.UpdateVal:FireServer(child , gui)--fire server

changing or adding stuff in gui should be done locally.

so I need to clone tool and change value from local script or something??

and why are you needing to fire to the server?
so both client and server know whats going on maybe…

is tool an actual item that the player can use, then yea you fire to server to get it

if its a gui element then you clone locally and change locally

so I need to change value from local script ??

do you know what particular line is not working properly?

I dont think any…is it because for loops that causing it??

for i , child in pairs(player.PlayerGui:WaitForChild("HotbarGui").Frame:GetChildren()) do--loop throuhg hotbar gui
				print("imsn")
				if child:IsA("ImageButton") then--chech if child is image buttton
					if child.ItemName.Value == ""  then--check if value(inside child) == empty string
						
						game.ReplicatedStorage.UpdateVal:FireServer(child , gui)--fire server

I think its this line beacuse all the hotbar start with empty strings

Is all of your code included? Please remove unnecessary prints and try to format it as best you can so that we can get to the root of the problem quickly :slight_smile:

I am not seeing anywhere you’re setting the Image property of those GUIs, so I am assuming not all code is being shown.

EDIT: What does the server do with this event? The way you are networking with the names of GUIs is very concerning, and likely very exploitable :open_mouth:

game.ReplicatedStorage.UpdateVal:FireServer(child , gui)--fire server

that sounds valid, also does it work the 2nd time you click or no?

you mean this:

game.ReplicatedStorage.UpdateVal.OnServerEvent:Connect(function(player , item , tool)
	
	
	
	item.ItemName.Value = tool.Name
	item.Image = tool.Image
	
end)

Im chaging the value

Nope still the same value…not changing at all

Im trying to make my code as neat as possible . Sorry if you had hard time reading my nasty code Im still learning :slight_smile:

think you can send a video? What do you mean it is still the same value? is it not changing at all?

If its not changing at all the 2nd time you need to recode the way you do change because you detect if its merely ="" then change

I want to send vid but how do I send vid without you guys downloading it

robloxapp-20210715-0001544.wmv (1.2 MB)

here is the vid