Help working with dynamic "if" statements!

Hello! I’m trying to get an if statement to recognize multiple classes. This is what i have so far but id doesn’t work and I’m stuck…


		local knifename = ply:GetActiveWeapon():GetClass()
		local knifetype = "bayonet", "tiger"
		local saleprice = 0

		if knifename == "csgo_"..knifetype.."_fade" then
			saleprice = 400000
		else
			saleprice = 150000
		end

		local DLabel = vgui.Create( "DLabel", Frame )
		DLabel:SetFont( "Default" )
		DLabel:SetPos( 20, 100 )
		DLabel:SetTextColor( Color(255,150,0,255) )
		DLabel:SetText( "Your Knife : "..knifename.."" )
		DLabel:SizeToContents()
		
	
		--local saleprice = 150000
		------------Main Menu Info Label-----Saleprice-----
		local DLabelPrice = vgui.Create( "DLabel", Frame )
		DLabelPrice:SetFont( "Default" )
		DLabelPrice:SetPos( 20, 140 )
		DLabelPrice:SetTextColor( Color(255,150,0,255) )
		DLabelPrice:SetText( "Sale Price : "..saleprice.."$" )
		DLabelPrice:SizeToContents()
		DLabelPrice:SizeToContentsY()

I Have also tried this:


local knifename = ply:GetActiveWeapon():GetClass()
		--local knifetype = "bayonet"
		local saleprice = 0

		if knifename == "csgo_bayonet_fade" or "csgo_bayonet_tiger"  then
			saleprice = 400000
		else
			saleprice = 150000
		end

		local DLabel = vgui.Create( "DLabel", Frame )
		DLabel:SetFont( "Default" )
		DLabel:SetPos( 20, 100 )
		DLabel:SetTextColor( Color(255,150,0,255) )
		DLabel:SetText( "Your Knife : "..knifename.."" )
		DLabel:SizeToContents()

		------------Main Menu Info Label-----Saleprice-----
		local DLabelPrice = vgui.Create( "DLabel", Frame )
		DLabelPrice:SetFont( "Default" )
		DLabelPrice:SetPos( 20, 140 )
		DLabelPrice:SetTextColor( Color(255,150,0,255) )
		DLabelPrice:SetText( "Sale Price : "..saleprice.."$" )
		DLabelPrice:SizeToContents()
		DLabelPrice:SizeToContentsY()

also the dynamic pricing works with a single knife but i need to add like 100 so that doesnt work either but neither of the codes that i have made seem to work. i got some version of the code working but then i for got to save it correctly or something idk.
Thanks in advance!

Multiple equivalency checks to one variable doesn’t work like that. Do:


if knifename == "thing" or knifename == "thing2" then

Awesome! Thanks! Ill test it now!

How about


local knifetypes = {"bayonet","tiger"}

for _, kt in pairs(knifetypes) do
	if knifename == "csgo_"..kt.."_fade" then
		saleprice = 400000
	else
		saleprice = 150000
	end
end

You can add as many types as you want into the knifetypes table

im new with lua so i dont know if it would work but i will certainly try it! thanks for the help!

You can just do table key lookups at that point. Also, you forgot a break.

i just tried this one and the one code_gs suggested and neither work. :frowning: ill include the code:

i changed knifename to curr_class and included the config file fyi


local curr_class = ply:GetActiveWeapon():GetClass()
		local saleprice = 0

		local knifetypes = {"bayonet","bowie"}

		for _, kt in pairs(knifetypes) do
			if curr_class == "csgo_"..kt.."_fade" then
				saleprice = 400000
			else
				saleprice = 150000
			end
		end

		local DLabel = vgui.Create( "DLabel", Frame )
		DLabel:SetFont( "Default" )
		DLabel:SetPos( 20, 100 )
		DLabel:SetTextColor( Color(255,150,0,255) )
		DLabel:SetText( "Your Knife : "..curr_class.."" )
		DLabel:SizeToContents()
		
	
		--local saleprice = 150000
		------------Main Menu Info Label-----Saleprice-----
		local DLabelPrice = vgui.Create( "DLabel", Frame )
		DLabelPrice:SetFont( "Default" )
		DLabelPrice:SetPos( 20, 140 )
		DLabelPrice:SetTextColor( Color(255,150,0,255) )
		DLabelPrice:SetText( "Sale Price : "..saleprice.."$" )
		DLabelPrice:SizeToContents()
		DLabelPrice:SizeToContentsY()

and code_gs’


local curr_class = ply:GetActiveWeapon():GetClass()
		local saleprice = 0

		if curr_class == _dp_inf then
			saleprice = 400000
		else
			saleprice = 150000
		end
		local DLabel = vgui.Create( "DLabel", Frame )
		DLabel:SetFont( "Default" )
		DLabel:SetPos( 20, 100 )
		DLabel:SetTextColor( Color(255,150,0,255) )
		DLabel:SetText( "Your Knife : "..curr_class.."" )
		DLabel:SizeToContents()
		
	
		--local saleprice = 150000
		------------Main Menu Info Label-----Saleprice-----
		local DLabelPrice = vgui.Create( "DLabel", Frame )
		DLabelPrice:SetFont( "Default" )
		DLabelPrice:SetPos( 20, 140 )
		DLabelPrice:SetTextColor( Color(255,150,0,255) )
		DLabelPrice:SetText( "Sale Price : "..saleprice.."$" )
		DLabelPrice:SizeToContents()
		DLabelPrice:SizeToContentsY()

also _dp_inf is defined in my config file:


_dp_inf = "csgo_bayonet_fade" or curr_class == "csgo_bayonet_tiger" or curr_class == "csgo_bowie_fade" or "csgo_bowie_tiger" or curr_class == "csgo_butterfly_fade" or curr_class == "csgo_butterfly_tiger" or curr_class == "csgo_falchion_fade" or curr_class == "csgo_falchion_tiger" or curr_class == "csgo_flip_fade" or curr_class == "csgo_flip_tiger" or curr_class == "csgo_gut_fade" or curr_class == "csgo_gut_tiger" or curr_class == "csgo_huntsman_fade" or curr_class == "csgo_huntsman_tiger" or curr_class == "csgo_karambit_fade" or curr_class == "csgo_karambit_tiger" or curr_class == "csgo_m9_fade" or curr_class == "csgo_m9_tiger" or curr_class == "csgo_daggers_fade" or curr_class == "csgo_daggers_tiger"

i may doing this completly wrong but i cant get it working. Thanks!

[EDIT] code_gs code DOES work for the first “class” but not the others

Yea, it was just a quick code, indeed a break is missing, thanks for pointing it out.

Try this method:


-- Outside function...
local knives = {
   ["csgo_blah"] = true,
   ["csgo_blah2"] = true,
   -- etc
}

-- Inside function...
local pActiveWeapon = ply:GetActive

if ( pActiveWeapon ~= NULL and knives[pActiveWeapon:GetClass()] ) then
   -- etc
end

.

[editline]27th July 2016[/editline]

Got it working! Thanks Everyone!