file.Append newline resulting in error

Hello FacePunch.

Everytime I try to use the newline tags (
or
– tried both), the console errors on me, on example using weapons.GetList(), I dont know if I simply wrote something wrong but… yeah…it works when I dont do it with …"
", then the code works fine.

Code:



for k, v in pairs(weapons.GetList()) do
	file.Append("weapontest.txt", v["Primary"]["Damage"].."
") -- Errors
end

for k, v in pairs(weapons.GetList()) do
	file.Append("weapontest.txt", v["Primary"]["Damage"]) -- Works fine
end


Error:



> for k, v in pairs(weapons.GetList()) do file.Append("weapontest.txt", v["Primary"]["Damage"].."
") end...
L 02/01/2017 - 12:47:32: Lua Error: 
[ERROR] lua_run:1: attempt to concatenate field 'Damage' (a nil value)
1. unknown - lua_run:1



[ERROR] lua_run:1: attempt to concatenate field 'Damage' (a nil value)
1. unknown - lua_run:1
 
12:47:32 Lua Error: [ERROR] lua_run:1: attempt to concatenate field 'Damage' (a nil value) 1. unknown - lua_run:1


This happens on any field I try in the Swep table. I hope you can help me with that!

Some weapon objects don’t have a “Primary” key so you’re trying to concatenate a nil value, which is specified in the error you get.


for i, wep in ipairs(weapons.GetList()) do
	local dmg = wep.Primary.Damage;
	if !(dmg) then continue; end
	file.Append("weapontest.txt", dmg .. "
");
end

I dont wanna sound mean, but please reread the thread, the function works fine WITHOUT the newline tags, ALL weapons output the damage value. when I add the newline stuff, THEN it fails.

Re-read what I said. You’re getting an error because you’re concatenating a string to a nil value. "
" isn’t a nil value so it has to be the weapon object.

How come my code works fine without "
" then?

I literally just said why.

Well, you said some weapons dont have the primary/damage key, but as said… How come my function does NOT error when its ran WITHOUT "
" but errors when it IS ran WITH "
"? What have newlines to do with a missing key?

If you aren’t concatenating, then there is no nil concatenation, and file.append will happily take nil as an argument. The error you are getting is nothing to do with calling file.append, its the concatenation it’s complaining about. If you are going to call people stupid when they are giving you the correct answer to your question, then don’t ask the question and instead remain in ignorance.

“You’re getting an error because you’re concatenating a string to a nil value.”

When did I ever say hes stupid?

You are basically saying he wasn’t capable of understanding your question first time round. To me that’s calling someone stupid.

If you are asking a question and immediately reject someone’s answer because you are ignorant of the error you are getting, feel free not to post your questions because obviously you know better than us.