Tirimiru Posted August 21, 2015 Share Posted August 21, 2015 (edited) So I'm not quite sure why I get this error -- I'm not sure how to initialize this value properly. The piece of code is working fine on the host, but when joining a server as a client, it crashes my game and shoots me that error 'Attempt to index local 'combat' (a nil value)' Here's the code where it's breaking in the modmain.lua:AddPlayerPostInit(function(player) local critchance = 0.1 local combat = player.components.combat local oldCalcDamage = combat.CalcDamage combat.CalcDamage = function(self, target, weapon, multiplier) local x, y, z = target.Transform:GetWorldPosition() multiplier = multiplier or self.damagemultiplier or 1 if weapon.prefab == "blackblade" and target and not target:HasTag("wall") and not target:HasTag("structure") then local fx = SpawnPrefab("slash_impact") fx.Transform:SetPosition(x, y, z) if math.random() <= critchance then fx = SpawnPrefab("critical_impact") fx.Transform:SetPosition(x, y+2, z) multiplier = multiplier*2 end end return oldCalcDamage(self, target, weapon, multiplier) endend) Edited August 22, 2015 by Tirimiru Link to comment Share on other sites More sharing options...
ZupaleX Posted August 22, 2015 Share Posted August 22, 2015 (edited) You cannot call components.combat on the client because it doesn't exist (and it applies to everything.)Call replica.combat instead. Edited August 22, 2015 by ZupaleX Link to comment Share on other sites More sharing options...
DarkXero Posted August 22, 2015 Share Posted August 22, 2015 (edited) And it will crash because oldCalcDamage would be nil. Then called on the return of the function.Because inst.replica.combat.CalcDamage is nil. What you have to do is stop the client from reaching the host code. LikeAddPlayerPostInit(function(player) if not GLOBAL.TheWorld.ismastersim then return end -- All code below this and inside the PostInit is host code -- Host code hereend)orif not GLOBAL.TheNet:GetIsServer() then returnend-- All code below this is host codeAddPlayerPostInit(function(player) -- Host code hereend)These structures are needed for "all_clients_require_mod = true" mods.As character mods are, since clients have to load the assets. For server only mods, these aren't needed. Edited August 22, 2015 by DarkXero Link to comment Share on other sites More sharing options...
Tirimiru Posted August 23, 2015 Author Share Posted August 23, 2015 Ahh I see I see! I should keep note on that -- I keep getting this same problem but I never know why XDThank you both for the help Link to comment Share on other sites More sharing options...
Tirimiru Posted August 23, 2015 Author Share Posted August 23, 2015 (edited) @DarkXeroHmm okay - so a new issue using this code.. If hitting mobs with no weapon a.k.a punching i get an error, "attempt to index local 'weapon'(a nil value)" I would like to know how to cancel out the whole calcdamage function if they're not equipping a weapon but I don't know how Edited August 23, 2015 by Tirimiru Link to comment Share on other sites More sharing options...
Tirimiru Posted August 23, 2015 Author Share Posted August 23, 2015 (edited) No worries, fixed it! Edit: Nope nevermind - haven't fixed it.. @_@Edit: Fixed it.. hopefully LOL nah i got it XD Edited August 23, 2015 by Tirimiru Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now