Can someone please explain this

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

make a pet merging system

  1. What is the issue? Include screenshots / videos if possible!

image

	print(Pet5.PetName.Value == Pet4.PetName.Value)
	print(Pet4.PetName.Value == Pet3.PetName.Value)
	print(Pet5.PetName.Value == Pet3.PetName.Value)
	print((Pet5.PetName.Value == Pet4.PetName.Value) == Pet3.PetName.Value)

Why does the forth line false while the other ones are true?

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

tried everything

This is the entire script if you needed it

--// Services
local MarketPlaceService = game:GetService("MarketplaceService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local SoundService = game:GetService("SoundService")
local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

--// Loading
local Player = Players.LocalPlayer
repeat wait() until Player:FindFirstChild("Loaded") and Player.Loaded.Value or Player.Parent == nil

if Player.Parent == nil then return end

--// Variables
local Data = Player.Data
local PlayerData = Data.PlayerData

local GameSettings = ReplicatedStorage["Game Settings"]
local Modules = ReplicatedStorage.Modules

local PetMultipliers = require(Modules.PetMultipliers)
local Multipliers = require(Modules.Multipliers)
local Utilities = require(Modules.Utilities)

local UI = Player.PlayerGui:WaitForChild("GameUI")
local Frames = UI.Frames

local Remotes = ReplicatedStorage.Remotes

local PetInventory = {} -- all pets will be added in this table

local PetFrame = Frames.Merger

local SelectedForMerge = {}

function SortInventory(SortTable, ObjectHolder)
	local TableToSort = SortTable ~= nil and SortTable or PetInventory -- so it can differentiate between trade and normal inventories

	table.sort(TableToSort, function(a,b)
		if not a or not b then return end

		if a.Multiplier ~= b.Multiplier then
			return a.Multiplier > b.Multiplier
		else
			return a.ID > b.ID
		end
	end)

	for Order,PetInfo in TableToSort do
		if not Data.Pets:FindFirstChild(PetInfo.ID) then
			table.remove(TableToSort, Order)
			continue
		end

		if not ObjectHolder then
			PetFrame.ObjectHolder[PetInfo.ID].LayoutOrder = Order + (Data.Pets[PetInfo.ID].Equipped.Value and 0 or 1000) -- order + 1000 if equipped
		else
			ObjectHolder[PetInfo.ID].LayoutOrder = Order + (Data.Pets[PetInfo.ID].Equipped.Value and 0 or 1000) -- order + 1000 if equipped
		end
	end
end

local CurrentlySelected = 0

function AddPet(PetInstance, SortTable, Parent) -- Creates a pet slot
	task.wait(0.1)
	local NewPet = script.PetTemplate:Clone()

	local PetModel = ReplicatedStorage.Pets[PetInstance.PetName.Value]:Clone()
	PetModel.Parent = NewPet.Display

	local MainPart = PetModel:FindFirstChild("MainPart")
	local Pos

	if not MainPart then
		warn(PetModel.Name.." does not have a MainPart, please add one by calling one of the parts 'MainPart'")

		for _,v in PetModel:GetChildren() do -- this here chooses a random part of the pet instead of the mainpart.
			if v:IsA("BasePart") then
				Pos = v.Position
				break
			end
		end
	else
		Pos = MainPart.Position
	end

	local Pos = PetModel.MainPart.Position
	local Camera = Instance.new("Camera")
	NewPet.Display.CurrentCamera = Camera
	PetModel:PivotTo(PetModel:GetPivot() * CFrame.Angles(0, math.rad(180), 0))
	Camera.CFrame = CFrame.new(Vector3.new(Pos.X + PetModel:GetExtentsSize().X * 1.5, Pos.Y, Pos.Z + 1), Pos)

	if not Parent then -- normal pet
		
		NewPet.Button.MouseButton1Click:Connect(function()
			local Result = NewPet.Selected
			local HowMuchPetsSelected = script.HowMuchPets
			
			print(Result.Value)
			
			if Result.Value == false then
				if HowMuchPetsSelected.Value >= 5 then return end
				Result.Value = true
				HowMuchPetsSelected.Value += 1
				script.Parent.PetsMerged.Text = tostring(HowMuchPetsSelected.Value.."/5 Pets Merged")
				NewPet.Equipped.Visible = true
				table.insert(SelectedForMerge, PetInstance)
			elseif Result.Value == true then
				Result.Value = false
				NewPet.Equipped.Visible = false
				HowMuchPetsSelected.Value -= 1
				script.Parent.PetsMerged.Text = tostring(HowMuchPetsSelected.Value.."/5 Pets Merged")
				table.remove(SelectedForMerge, table.find(SelectedForMerge, PetInstance))
			end
			Utilities.Audio.PlayAudio("Click")
		end)
	end
	NewPet.Name = PetInstance.Name
	NewPet.Parent = Parent == nil and PetFrame.ObjectHolder or Parent

	if SortTable == nil then
		PetInventory[#PetInventory+1] = {ID = PetInstance.Name, Multiplier = ReplicatedStorage.Pets[PetInstance.PetName.Value].Settings.Multiplier.Value}
	else
		SortTable[#SortTable+1] = {ID = PetInstance.Name, Multiplier = ReplicatedStorage.Pets[PetInstance.PetName.Value].Settings.Multiplier.Value}
	end
	
	return NewPet
end

function MergePets(PetsTable)
	local HowMuchPets = 0
	local Pet1
	local Pet2
	local Pet3
	local Pet4
	local Pet5
	
	for i, v in PetsTable do
		HowMuchPets += 1
		if i == 1 then
			Pet1 = v
		elseif i == 2 then
			Pet2 = v
		elseif i == 3 then
			Pet3 = v
		elseif i == 4 then
			Pet4 = v
		elseif i == 5 then
			Pet5 = v
			break
		end
	end
	
	print(HowMuchPets, Pet1.PetName.Value, Pet2.PetName.Value, Pet3.PetName.Value, Pet4.PetName.Value, Pet5.PetName.Value)
	
	print(Pet5.PetName.Value == Pet4.PetName.Value)
	print(Pet4.PetName.Value == Pet3.PetName.Value)
	print(Pet5.PetName.Value == Pet3.PetName.Value)
	print((Pet5.PetName.Value == Pet4.PetName.Value) == Pet3.PetName.Value)
	
	if (((Pet5.PetName.Value == Pet4.PetName.Value) == Pet3.PetName.Value) == Pet2.PetName.Value) == Pet1.PetName.Value then
		print(HowMuchPets, Pet1.PetName.Value, Pet2.PetName.Value, Pet3.PetName.Value, Pet4.PetName.Value, Pet5.PetName.Value)
	end
end

script.Parent.Merge.MouseButton1Click:Connect(function()
	MergePets(SelectedForMerge)
end)

for _,v in Data.Pets:GetChildren() do -- load pets on join
	coroutine.wrap(function()
		AddPet(v)
	end)()
end

coroutine.wrap(function()
	task.wait(0.5)
	SortInventory(PetInventory, script.Parent.ObjectHolder) -- made it wait 0.5 seconds before it sorted inventory because the pets are not yet loaded
end)()

function OnPetAdded(Child) -- this function is ran when a pet is added, which updates the counters & adds a new pet ui instance
	AddPet(Child)
	SortInventory(PetInventory, script.Parent.ObjectHolder)
end

function OnPetRemoved(Child)
	PetFrame.ObjectHolder[Child.Name]:Destroy()
	SortInventory(PetInventory, script.Parent.ObjectHolder)
end

Data.Pets.ChildAdded:Connect(OnPetAdded)
Data.Pets.ChildRemoved:Connect(OnPetRemoved)

also can anyone improve the if statement

You put brackets around so it evaluates it first and becomes something like

true == Pet3Name "Bob"

Which will be false.

Oh, i should’ve known :confused:

Ok but how do i check if the values of 5 variables are the same

Better to use a dictionary.

Loop then if the key is the same name then add the value by 1.
{
[“Bob”] = 1
}

Wait i just found a way and it worked but i will put your first reply as a solution because you explained to me that PetName == PetName == PetName is actually false == Petname and that what the title of this topic requested

1 Like

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