Switching to weapon in table

Im trying to make it where if you run pick up a weapon and its in the table then it will be switched to that. I havent worked on it in a while but I thought I had it working, now its throwing me an error
[Lua]
function GM:PlayerCanPickupWeapon(ply, wep)

local WepGroup_pistol = {
"weapon_glock",
"weapon_deagle",
}

if (ply:GetActiveWeapon():GetClass() == "weapon_knife") && (table.HasValue(WepGroup_pistol, wep:GetClass())) then  
    ply:SelectWeapon("wep")
end  

end
[/Lua]

Mind telling us the error?

yeah sorry, I was grabbing it and I just my track of thoughts

[Lua]
init.lua:741: ‘end’ expected (to close ‘function’ at line 724) near ‘if’
[/Lua]
sorry

Apparently you’re not posting all of the relevant code. Might want to have a look at line 741.

wep should not be in “” or ‘’ as it’s not a string.

The last entry in a table does not have a comma either.

[lua]
function GM:PlayerCanPickupWeapon(ply, wep)

local WepGroup_pistol = {  
"weapon_glock",  
"weapon_deagle"
}  
  
if (ply:GetActiveWeapon():GetClass() == "weapon_knife") && (table.HasValue(WepGroup_pistol, wep:GetClass())) then    
    ply:SelectWeapon(wep:GetClass())  
end    

end
[/lua]

yeah sorry im being a tard today line 741 is if (ply:GetActiveWeapon():GetClass() == “weapon_knife”) && (table.HasValue(WepGroup_pistol, wep:GetClass())) then
and ill try that now Averice thanks but im not seeing the extra bracket?

You’re right, I didn’t count the opening bracket on table.HasValue, my fault, glad you picked up on it.

Did you do what the error message told you? It helps to fix fatal errors first before going for logical ones.

ahhh this is really buggin me, I cant see whats wrong with it and it is this section of code cus when I comment it out the gamemode works fine

[editline]05:30PM[/editline]

what do you mean do what the error message told me?, thats what im trying to figure out

You’re calling wep:GetClass() on a table that’s technically just a bunch of strings. Shouldn’t you just check if the string of the weapon they picked up’s class is equal to one of the strings in your table? Just surmising here.

[lua]
table.HasValue(WepGroup_pistol, wep:GetClass())
[/lua]

…yes, that is in his code as one of the conditionals to switch to the picked up weapon. Are you trying to tell me that I was wrong? Posting the code in question as an answer to the question isn’t very helpful for me (since I obviously didn’t know to begin with).

wep:GetClass() returns the weapons class name as a string, he is checking the WepGroup_Pistol table if it has a match to the class name of the weapon he is trying to pick up.

No, I was just showing you that he was already doing that.

I’m still not seeing where making a table with a bunch of weapon names in it = entities that automatically have the class of a weapon. Are you saying that just because the string in the table is the same as a default weapon name, it will be treated as an entity class automatically?

weapon:GetClass() returns a string. If the string is in the table, then yes, the table has that class of the weapon inside.

This is the full function if it helps
[lua]function GM:PlayerCanPickupWeapon(ply, wep)

local WepGroup_pistol = {         //doing the table of pistols, havent finished just a test
"weapon_glock",
"weapon_deagle"
}

if (ply:HasWeapon(wep:GetClass())) then return false end      //Stops you from picking up the same weapons

if (table.HasValue(WepGroup_pistol, wep:GetClass())) then     //stops you from picking up the same weapon group(pistol)
    for _, v in pairs(WepGroup_pistol) do
        if(ply:HasWeapon(v)) then return false end
    end
end

return true    

if (ply:GetActiveWeapon():GetClass() == "weapon_knife") && (table.HasValue(WepGroup_pistol, wep:GetClass())) then  
    ply:SelectWeapon(wep)
end  

end[/lua]
[editline]09:36PM[/editline]

but the problem is the last if statment, still giving me the same error

The error comes from you having code after a return statement. Everything after return true has to be moved. Of course it will stop making sense if you move it but that’s just the way things are.

lol makes sense, I should of put the whole function first, sorry for wasting your time. Ill know next time :slight_smile:

[editline]09:44PM[/editline]

and thanks alot for the help