How to stop strings having the same value?

I am working in one equip system for one simulator using StringValues and it works. The problem is you can keep equiping the same thing ( all 3 strings having the same value)

GIF:

The script is something like this:

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

Note: SelectedPetValue.Value is one string when you click the pet gui you want this gui changes with the pet ID and if you click the Equip button the slot will have this String Value

2 Likes

is there any way to change the string with same value and keep changing others without giving the same value for all?

Well you are looping through all of them so it changed all, if you want a certain one you could have either a table of those strings and just pass in a name or an number increasing and do :FindFirstChild(“blahblah”…n)

Not sure what you mean though please elaborate

I am using break for change only 1 the problem is I dont know what I could use for see if the string alr got that value for not skip to others like if == something then (not skip to other string and try to find the string with that value) I cant use v.Value[SelectedPetValue.Value]

if stringValue.Value ~= string then

end

String being for what you don’t want it to have

1 Like

I use if v.Value == “” then for find empty strings

and v.Value ~= SelectedPetValue.Value for find strings with values

So you basically don’t want the player to be able to equip the same pet more than once?

yes I just want to equip 1 time that value and if the player alr have that value equiped not change it if the player not have that value will skip into other StringValue.

Alright give me one min I’m editing your code

1 Like

Quick question though.
Let’s say the first value is “Pet1”, and now the player wants to equip “Pet2”. should it remove “Pet1”, or skip to the next empty value?

the problem is not empty values. The problem is if I have the alr that value I want to not make the player able to equip it again

this is what happen when you click 3 times on the equip button (same pet)

this is what happen when you equip other value(works)

local function isEquippedAlready(SelectedPet)
	for _, v in pairs(game.Players.LocalPlayer.Main_Values.PlrSettings:GetChildren()) do
		if v:IsA("StringValue") then
			if v.Value == SelectedPet.Value then
				return true
			end
		end
	end
	return false
end

script.Parent.MouseButton1Click:Connect(function()
	if isEquippedAlready(SelectedPetValue) == false then
		for _, v in pairs(game.Players.LocalPlayer.Main_Values.PlrSettings:GetChildren()) do
			if v:IsA("StringValue") then
				if v.Value == "" then
					v.Value = SelectedPetValue.Value
					break
				end
			end
		end
	end
end)

Try this. I don’t know if this is what you meant, let me know if anything should be changed.

doesnt work… I tried but nothing changes

I edited the code, try it now again

Doesnt work I will try to find one way to find the value.

You don’t have a check to see if the string already existed or not.

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

				break
			elseif v.Value ~= SelectedPetValue.Value then
				v.Value = SelectedPetValue.Value
			
				break
			end
		end
	end
end)

It works only in one part now is checking the same value and doesnt change but I cant change other string with a new value

mb i forgot about that, just edited the script

there are some optimization i could make im just… you know, lazy to do so.

I tried to use the edited version but still does the same thing

cant we just like if the value is == SelectedPetValue.Value then the script skip into the other string?