Elseif part never happens

Trying to make the elseif part of the script work, but it doesn’t for some reason. Tried playing around with the code and look through the forums but nothing worked hence I’m here.

--local player = game.Players.LocalPlayer
local framevaluef = script.Parent.FrameValues

for _, descendant in pairs(framevaluef:GetChildren()) do
	if descendant:IsA("ObjectValue") then
		descendant.Changed:Connect(function()
			for _, child in pairs(script.Parent:GetChildren()) do
				if child:IsA("Frame") then
					if child.Name == descendant.Name and descendant.Value ~= nil then
						child.ImageButton.CharacterDisplay.DisplayScript.Enabled = false
						child.ImageButton.CharacterDisplay.Shotgun:Destroy()
						local car = descendant.Value:Clone()
						car.Parent = child.ImageButton.CharacterDisplay
						car.Name = "Shotgun"
						task.wait()
						child.ImageButton.CharacterDisplay.DisplayScript.Enabled = true	
					elseif  child.Name == descendant.Name and descendant.Value == nil then
						child.Visible = false
						print("setting them to invisible")	
							
					end
				end
			end
		end)
	end
end

Any help would be great. Thanks!

You could do further debug statements

--local player = game.Players.LocalPlayer
local framevaluef = script.Parent.FrameValues

for _, descendant in pairs(framevaluef:GetChildren()) do
	if descendant:IsA("ObjectValue") then
		descendant.Changed:Connect(function()
			for _, child in pairs(script.Parent:GetChildren()) do
				if child:IsA("Frame") then
				  print("DEBUG child NAME: ", child.Name)
				  print("DEBUG descendant NAME: ", descendant.Name)
				  print("DEBUG descendant value", descendant.Value)
				  
					if child.Name == descendant.Name and descendant.Value ~= nil then
						child.ImageButton.CharacterDisplay.DisplayScript.Enabled = false
						child.ImageButton.CharacterDisplay.Shotgun:Destroy()
						local car = descendant.Value:Clone()
						car.Parent = child.ImageButton.CharacterDisplay
						car.Name = "Shotgun"
						task.wait()
						child.ImageButton.CharacterDisplay.DisplayScript.Enabled = true	
					elseif  child.Name == descendant.Name and descendant.Value == nil then
						child.Visible = false
						print("setting them to invisible")	
							
					end
				end
			end
		end)
	end
end

This will help you see what is being printed. Make sure your names are exact for exact or else it wont work

1 Like

Here, I cleaned it up with some guard clauses. You also do not need to check if a variable is equal to nil, you just need to check for the variable itself.

For example, someVariable in a boolean expression reduces to true if it exists, and false if it does not exist (is equal to nil)

for _, descendant in pairs(framevaluef:GetDescendants()) do

	if not descendant:IsA("ObjectValue") then continue end

	descendant.Changed:Connect(function()

		for _, child in pairs(script.Parent:GetChildren()) do

			if not child:IsA("Frame") then continue end

			if child.Name ~= descendant.Name then continue end


			if descendant.Value then
				child.ImageButton.CharacterDisplay.DisplayScript.Enabled = false
				child.ImageButton.CharacterDisplay.Shotgun:Destroy()
				local car = descendant.Value:Clone()
				car.Parent = child.ImageButton.CharacterDisplay
				car.Name = "Shotgun"
				task.wait()
				child.ImageButton.CharacterDisplay.DisplayScript.Enabled = true	

			else
				child.Visible = false
				print("setting them to invisible")				
			end
		
		end

	end)

end
1 Like

This did help and I think I know what the issue is. Its descendant.Changed. The ones that are nil don’t really ever change, they are always nil. So now I need to figure a way to make them change and then change back to nil.

1 Like

Glad to see you are able to narrow it down. If you still cant come up with a solution, you could supply us your other code function where you set them to nil?

--

skinsfolder.FrameValues.Frame1.Value = game.ReplicatedStorage.MenuCars["Lightning McQueen"]
	skinsfolder.Frame1.ImageButton.SkinName.Value = skinsfolder.FrameValues.Frame1.Value.Name
	---------------------------------------------------------------------------------------------------------------------------
	---------------------------------------------------------------------------------------------------------------------------
	skinsfolder.FrameValues.Frame2.Value = game.ReplicatedStorage.MenuCars["Lightning McQueen RS"]
	skinsfolder.Frame2.ImageButton.SkinName.Value = skinsfolder.FrameValues.Frame2.Value.Name
	---------------------------------------------------------------------------------------------------------------------------
	skinsfolder.FrameValues.Frame3.Value = game.ReplicatedStorage.MenuCars["Lightning McQueen Green"]
	skinsfolder.Frame3.ImageButton.SkinName.Value = skinsfolder.FrameValues.Frame3.Value.Name
	---------------------------------------------------------------------------------------------------------------------------
	for _, descendant in pairs(skinsfolder.FrameValues:GetChildren()) do
		if descendant:IsA("ObjectValue") and descendant.Value == nil then
			descendant.Value = game.ReplicatedStorage.MenuCars["Lightning McQueen Green"]
			descendant.Value = nil
		end
	end

This is pretty much it. Now it works perfectly, probably not the most convenient way of doing this, but it does work and gets the job done now.

1 Like