• Problem with parameters
    13 replies, posted
Basicily im triying to create a crafting system But every time i try to call the Craft item function i get Init.lua:65) Attempt to call method "CraftItem" (a nil value) i do have check twice the functions and i still cant see the problem if you got some spare time to check it then i would really appreciate it (: Anyway heres the code SH_Crafts.lua [LUA]GM.Crafts = {} for _, filename in pairs( file.Find( "../" .. GM.Folder .. "/gamemode/playerclasses/*.lua" ) ) do CRAFT = {} AddCSLuaFile( "playerclasses/" .. filename ) include( "playerclasses/" .. filename ) GM.Crafts[CRAFT.Id] = CRAFT CRAFT = nil end function GM:CraftByName( name ) for _, craft in pairs( GAMEMODE.Crafts ) do if ( craft.Name == name ) then return craft end end end function GM:GetCraft( ply ) if ( SERVER ) then return self.Craft end end function GM:CraftItem( ply, name ) local lvl = ply:GetCraftLevel for _, craft in pairs( GAMEMODE.Crafts ) do if ( craft.Name == name ) then for _, CraftLevel in ipairs( craft.RequiredLevel ) do for _, CraftXPReward in ipairs( craft.XPReward ) do for _, CraftItems in ipairs( craft.RequiredItems ) do if ply:HasItem(CraftItems) then if lvl >= CraftLevel then ply:RemoveItem(CraftItems,1) ply:AddCraftXP(CraftXPReward) end end end end end end end[/LUA] ShotgunCraft.lua [LUA] CRAFT.Name = "ShotgunCraft" CRAFT.Id = 7 CRAFT.RequiredLevel = 6 CRAFT.XPReward = 80 CRAFT.ChanceToFail = 2 CRAFT.RequiredItems = { "item_Wood", "item_Iron" } CRAFT.Reward = { "item_food_MeatCan" }[/LUA]
Let the list of issues I have with this begin: 1) You haven't showed us the init.lua function where your error is generating from, so we can't help you with that problem until you do 2) That first loop, just no. Stop. A) You are creating a new GLOBAL table every time you hit a new member of the table. B) Then you are setting the same table to nil afterwards, with apparently not doing anything with it. 3) Fix your indentation. You will have a much, much easier time reading and debugging your code if you indent correctly. 4) Why is it necessary to have a triple nested loop. I don't like doing a single nested loop, but you have four inside one another, you can run into speed issues if you call that fast enough. You can easily do what (I think at least) you're trying to do in that nested loop by referencing each crafted item by the key in the CRAFTS table. 5) Depending on what file gets opened first, your CRAFTS table may be empty. Make sure that SH_Crafts is loaded before your other craft files or else the table may overwrite itself. 6) Line 30 of SH_Crafts.lua - you don't have any argument parenthesis on "GetCraftLevel", it should be "local lvl = ply:GetCraftLevel()"
Do not unreference CRAFT, perform table.Copy on it, thats what garry does for registering sweps etc.
Well after playing a bit and listening your sugerences I THINK i cleaned most of the code but im still getting the problem calling the function it prints the same thing this is the function im using to call the CraftItem function [LUA] function test(ply) ply:CraftItem("ShotgunCraft") end concommand.Add("test",test) [/LUA] and here is the rest of the code [LUA] GM.Crafts = {} for _, filename in pairs( file.Find( "../" .. GM.Folder .. "/gamemode/Crafts/*.lua" ) ) do AddCSLuaFile( "Crafts/" .. filename ) include( "Crafts/" .. filename ) end function CraftByName( name ) for _, craft in pairs( GAMEMODE.Crafts ) do if ( craft.Name == name ) then return craft end end end function GetCraft( ply ) if ( SERVER ) then return self.Craft end end if ( SERVER ) then function CraftItem( name ) for _, craft in pairs( GAMEMODE.Crafts ) do if ( craft.Name == name ) then if ply:HasItem(craft.RequiredItems) then ply:ChatPrint("the function is working") end end end end [/LUA]
okay so well im on my last problem every time i try to call the function it just nothing happends nothing shows on the console and not on the screen [LUA] function testcraft(ply) CraftItem(ply,"ShotgunCraft") end concommand.Add("CRAFTtest",testcraft) [/LUA] and here is the rest of the code [LUA] function CraftItem( ply, name ) for _, craft in pairs( GAMEMODE.Craftsss ) do if ply:HasItem(craft.RequiredItems) then ply:RemoveItem(craft.RequiredItems,1) ply:GiveItem(craft.Reward) ply:AddCraftXP(craft.XPReward) ply:ChatPrint("LOL WORKS") end end end end [/LUA]
[QUOTE=werewolf0020;30753658]okay so well im on my last problem every time i try to call the function it just nothing happends nothing shows on the console and not on the screen [LUA] function testcraft(ply) CraftItem(ply,"ShotgunCraft") end concommand.Add("CRAFTtest",testcraft) [/LUA] and here is the rest of the code [LUA] function CraftItem( ply, name ) for _, craft in pairs( GAMEMODE.Craftsss ) do if ply:HasItem(craft.RequiredItems) then ply:RemoveItem(craft.RequiredItems,1) ply:GiveItem(craft.Reward) ply:AddCraftXP(craft.XPReward) ply:ChatPrint("LOL WORKS") end end end end [/LUA][/QUOTE] [LUA] function testcraft(ply) ply:CraftItem("ShotgunCraft") end concommand.Add("CRAFTtest",testcraft) [/LUA] and here is the rest of the code [LUA] function _R.Player:CraftItem( name ) for _, craft in pairs( GAMEMODE.Craftsss ) do if self:HasItem(craft.RequiredItems) then self:RemoveItem(craft.RequiredItems,1) self:GiveItem(craft.Reward) self:AddCraftXP(craft.XPReward) self:ChatPrint("LOL WORKS") end end end [/LUA] Try that
][LUA] function testcraft(ply) ply:CraftItem(ply:CraftByName("ShotgunCraft")) end concommand.Add("CRAFTtest",testcraft) [/LUA] [LUA] function meta:CraftItem( craft ) for _, craft in pairs( GAMEMODE.Craftsss ) do if self:HasItem(craft.RequiredItems) then self:RemoveItem(craft.RequiredItems,1) self:GiveItem(craft.Reward) self:AddCraftXP(craft.XPReward) self:ChatPrint("LOL WORKS") else self:ChatPrint("NO ITEM") end end end end [/LUA] no matter what i do even if i have the items the game keeps telling me i dont ahve them is this even searching the data in the shotguncraft table?
You don't seem to be putting that function in the player table in the first place. do something like [code] local Player = FindMetaTable( "Player" ) function Player:CraftItem( craft ) blah blah blah end [/code]
alredy definied that i just dident show it on the code i pasted
That makes it difficult to discern the real issue I'm afraid =\ These errors sometimes pop up if something else in your script is broken. Do you have any other seemingly unrelated lua errors? A syntax error in your init.lua file will usually prevent any functions in that file from running, in my experience.
Yep i checked and nothing else shows up
Anyone?
Well in my experience that error can only mean two things: 1 - Somewhere in that particular file, where it is defined, there is some sort of error preventing the script from loading 2 - The function is not being correctly added to the player's method table Aside from those possibilities I have no clue.
Shouldn't 'GAMEMODE.Craftsss' be 'GAMEMODE.Crafts'
Sorry, you need to Log In to post a reply to this thread.