MySQL error, help!

Right, so i’ve got this gamemode which i’ve added an MySQL savings system to, it works fine except that when i wanna get a networked integer it says: “trying to compare number with string” important code parts are here:


 

if (ply:GetNWInt("points")) > 0 then // error occurs here!

query = "CREATE TABLE player_skills ( unique_id varchar(255), points int, jump int, speed int, hp int )"  // Creates the table when a player joins

sql.Query( "INSERT INTO player_skills (unique_id, points, jump, speed, hp)VALUES ('"..steamID.."', 1, 0, 0, 0)" ) // Happens when a new player joins

local unique_id = ply:GetNWString ("SteamID")
	local points = ply:GetNWInt("points")
	local jump = ply:GetNWInt("jump")
	local speed = ply:GetNWInt("speed")
	local hp = ply:GetNWInt("hp")
sql.Query("UPDATE player_skills SET points = "..points..", jump = "..jump..", speed = "..speed..", hp = "..hp.." WHERE unique_id = '"..unique_id.."'") // saves the values

local skills = sql.QueryValue("SELECT skills FROM player_skills WHERE unique_id = '"..steamID.."'") ply:SetNWInt("points", skills) // retrieves the values



Thank you.

Try replacing the first line with this:



if (ply:GetNWInt("points") > 0) then // error occurs here!


print(type(ply:GetNWInt(“points”)))
Put this line before anything else in the script, it tells you the datatype of the networked int. It should print ‘number’.

So that’d say in the initial spawn function?

function GM:PlayerInitialSpawn(ply)
print(type(ply:GetNWInt(“points”)))
end

right?

No. I mean put it before the first line in wherever the code snippet is in the OP.

print(type(ply:GetNWInt(“points”)))
if (ply:GetNWInt(“points”)) > 0 then // error occurs here!

Oh, ok, thanks alot!

[editline]08:06PM[/editline]

Sorry, but it prints “string”

try print(ply:GetNWInt(“points”)) instead, see if it’s a string that just contains the number.

Try and get used to adding print lines and things in sensible places to get a good analysis and diagnosis of the problem.

That was in it already, which means it looks like:
print(type(ply:GetNWInt(“points”)))
print(ply:GetNWInt("points))

[editline]08:28PM[/editline]

What it prints in console is:
string
1
error: trying to compare string with number

On the line with the error, try replacing it with this:

if tonumber(ply:GetNWInt(“points”)) > 0 then // error occurs here!

tonumber(somestring) will take a string that only has numbers in it and output the number equivalent. Give it “3.14” and you get 3.14. Give it “5” and you get 5.

Ah, i will test that, hope it works crosses fingers :stuck_out_tongue:

[editline]09:32PM[/editline]

It worked, thanks alot, even though i did add it at the retrieve data function, which makes it easier to work with :slight_smile: