How to stop strings having the same value?

Equip_Button.Button.MouseButton1Click:Connect(function()
	local settingsToChange 
	for _, v in pairs(plr.Main_Values.PlrSettings:GetChildren()) do
		if v:IsA("StringValue") then
			if v.Value == SelectedPetValue.Value then
				return
			elseif not settingsToChange or settingsToChange.Value ~= ""  then
				if v.Value == "" then
					settingsToChange = v
				else
					settingsToChange = v
				end
			end
		end
	end
	settingsToChange = SelectedPetValue.Value
end)

I shortened the code but still couldn’t locate the problem.

darn why do I only notice there is a problem with my code after I posted it. It’s edited.

if you do if v.Value == SelectedPetValue.Value then
return

you cant even equip one value

Someone knows how I could only skip the into the next string if his value is the same was the variable.Value???

I did one small change. But now I can add new values (sorry for the delay I was afk)

Equip_Button.Button.MouseButton1Click:Connect(function()
	local settingsToChange 
	for _, v in pairs(plr.InventoryValues.AllPlrValues:GetChildren()) do
		if v:IsA("StringValue") then
			if v.Value == SelectedPetValue.Value then
				print("test")
				return
			elseif not settingsToChange or settingsToChange.Value ~= ""  then
				if v.Value == "" then 
					v.Value = SelectedPetValue.Value
					break
				else
					v.Value = SelectedPetValue.Value
					break
				end
			end
		end
	end
	settingsToChange = SelectedPetValue.Value
end)

Any way I could do for skip into the next empty value or change any random value not == to the Value alr used? I alr tried everything.

what you could do is use a for loop within a function and make it return true if there is one with the same string or by default return false if not

local function checkPet(slot, value)
	for k,v in pairs(plr.Main_Values.PlrSettings:GetChildren()) do
		if (v.Value == value and v ~= slot) then
			return true
		end
	end
	return false
end

--Then you can use an if statement like this

if (not checkPet(slot1, value)) then
	
end

If this helps/fixes youir problem please set as solution! :smiley:

local folder = workspace.Folder --Folder of StringValue instances.

local function onStringChanged(stringValue, newString)
	if newString ~= "" then
		for _, otherString in ipairs(folder:GetChildren()) do
			if otherString:IsA("StringValue") then
				if otherString ~= stringValue then
					if otherString.Value == newString then
						stringValue.Value = ""
						return
					end
				end
			end
		end
	end
end

for _, stringValue in ipairs(folder:GetChildren()) do
	if stringValue:IsA("StringValue") then
		stringValue.Changed:Connect(function(newString)
			onStringChanged(stringValue, newString)
		end)
	end
end

This will prevent a folder of StringValue instances from storing the same string (ignoring empty string values).

Nothing changed. I tried to take in @Imaginalities script and I changed a little bit and it works if I had only 1 string.

1 string Exemple:

If I had 3 values exemple:

Is like the script doesnt want to see all values inside the player and just stay with 1 string.

The script is something like this:

Equip_Button.Button.MouseButton1Click:Connect(function()
	wait()
	local settingsToChange 
	for _, v in pairs(plr.InventoryValues.AllPlrValues:GetChildren()) do
		if v:IsA("StringValue") then
			if v.Value == SelectedPetValue.Value then
				
				return
			elseif not settingsToChange or settingsToChange.Value ~= ""  then
				if v.Value == "" then 
					v.Value = SelectedPetValue.Value
					print(v.Value, SelectedPetValue.Value)
					break
				else
					v.Value = SelectedPetValue.Value
					print(v.Value, SelectedPetValue.Value)
					break
				end
			end
		end
	end
	settingsToChange = SelectedPetValue.Value
end)

I have tried that code and didnt help bc I need to change the String.Value inside the pairs just like the code in the last reply.

Any way I could like skip into other string?