Hi, so I'm doing this kinda blacklist thing, but having some issues with converting the string and checking the tables value.
Basically what it is supposed to do, is that it takes the JSON string stored in a .txt file, converts it to a table, check if the model the player wants to add is in the table, if it isn't, it will add it, but if it is, it will not add it. After that it converts back to a JSON string, and into the .txt file where it will be stored again. ( Sorry for that long line )
But it seems like the table.HasValue always returns false, so it keeps adding the same model over and over.
[lua]function CheckBlackList()
local ModelBeingChecked = ply:GetEyeTrace().Entity:GetModel()
BannedProps_Beta = {}
local JsonTable = file.Read("betaprop/blacklist.txt", "DATA")
local Table = util.JSONToTable( JsonTable )
table.insert( BannedProps_Beta, Table )
if(not table.HasValue( BannedProps_Beta, ModelBeingChecked )) then
table.insert( BannedProps_Beta, ModelBeingChecked )
local TableToJson = util.TableToJSON( BannedProps_Beta )
file.Write("betaprop/blacklist.txt", TableToJson )
print("Prop has been added to the blacklist")
else
local TableToJson = util.TableToJSON( BannedProps_Beta )
file.Write("betaprop/blacklist.txt", TableToJson )
print("Already stored")
end
end
end
end )
[/lua]
This is the table after adding 2 different props, and using PrintTable:
[lua]1:
1 = models/props_borealis/bluebarrel001.mdl
2 = models/props_c17/furniturebathtub001a.mdl[/lua]
And this is the JSON string after adding 2 different props:
[lua]{"1":{"1":"models/props_borealis/bluebarrel001.mdl"},"2":"models/props_c17/furniturebathtub001a.mdl"}[/lua]
This is my first time using JSON strings, so please bare with me if this is an obvious mistake :V
On my phone but your first if statement looks weird. You should be using the and operator not a comma.
change
[LUA]table.insert( BannedProps_Beta, Table )[/LUA]
to
[LUA]table.Merge( BannedProps_Beta, Table )[/LUA]
There's also other issues with the code.
[QUOTE=edgarasf123;45563589]change
[LUA]table.insert( BannedProps_Beta, Table )[/LUA]
to
[LUA]table.Merge( BannedProps_Beta, Table )[/LUA]
There's also other issues with the code.[/QUOTE]
Erhh dammit, what kind of issues?
[B]EDIT[/B]:
My if statement went wrong, as I accidently deleted the part while pasting it here, it should be:
[LUA]if(not table.HasValue( BannedProps_Beta, ModelBeingChecked )) then [/LUA]
[QUOTE=The Beta;45563671]Erhh dammit, what kind of issues?
[B]EDIT[/B]:
My if statement went wrong, as I accidently deleted the part while pasting it here, it should be:
[LUA]if(not table.HasValue( BannedProps_Beta, ModelBeingChecked )) then [/LUA][/QUOTE]
There I rewrote the script for you
[LUA]function CheckBlackList()
local ModelBeingChecked = LocalPlayer():GetEyeTrace().Entity:GetModel() -- changed ply to LocalPlayer because it wasn't defined.
local BannedProps_Beta = util.JSONToTable( file.Read("betaprop/blacklist.txt", "DATA") ) -- writing directly to one variable without creating other useless variables
if not BannedProps_Beta then -- check if json file was valid
Error("Invalid blacklist file")
return nil
end
if not table.HasValue( BannedProps_Beta, ModelBeingChecked ) then -- check if your model is not in a table
BannedProps_Beta[#BannedProps_Beta + 1] = ModelBeingChecked
file.Write("betaprop/blacklist.txt", util.TableToJSON( BannedProps_Beta ) )
print("Prop has been added to the blacklist")
else -- If exist we don't need to rewrite file with same values
print("Already stored")
end
end[/LUA]
Thank you
Sorry, you need to Log In to post a reply to this thread.