kikos500 Posted July 13, 2016 Share Posted July 13, 2016 well this is a script that creates a table to store inside it players stats like cash wins , points ,etc but the problem is when i reconnect the script bugs out and im back at 1 like if i played 10 maps when i reconnect im back at 1 i want it to save function start() executeSQLQuery("CREATE TABLE IF NOT EXISTS stats_p (name STRING,serial STRING,played INT,wins INT,points INT,cash INT,kills INT,color STRING,wheel INT,wheelc STRING)") end addEventHandler("onResourceStart", root, start) function join() local name = getPlayerName(source) local serial = getPlayerSerial(source) local tables = executeSQLQuery("SELECT name,played,wins,points,cash,kills FROM stats_p WHERE serial=?",serial) if #tables == 0 then executeSQLQuery("INSERT INTO stats_p (name,serial,played,wins,points,cash,kills,color,wheel,wheelc) VALUES(?,?,?,?,?,?,?,?,?,?)",name,serial,1,1,1,1000,1,"#FFFFFF",1,"#FFFFFF") setElementData(source,"played",1,true) setElementData(source,"wins",1,true) setElementData(source,"points",1,true) setElementData(source,"cash",1000,true) setElementData(source,"kills",1,true) setElementData(source,"color","#FFFFFF",true) setElementData(source,"wheel",1,true) setElementData(source,"wheelc","#FFFFFF",true) else setElementData(source,"played", tables[1].played,true) setElementData(source,"wins", tables[1].wins,true) setElementData(source,"points", tables[1].points,true) setElementData(source,"cash", tables[1].cash,true) setElementData(source,"kills", tables[1].kills,true) setElementData(source,"color",tables[1].color,true) setElementData(source,"wheel",tables[1].wheel,true) setElementData(source,"wheelc",tables[1].wheelc,true) end end addEventHandler("onPlayerJoin", getRootElement(), join) function left() local played = getElementData(source,"played") local wins = getElementData(source,"wins") local points = getElementData(source,"points") local cash = getElementData(source,"cash") local kills = getElementData(source,"kills") local color = getElementData(source,"color") local wheel = getElementData(source,"wheel") local wheelc = getElementData(source,"wheelc") local name = getPlayerName(source) local serial = getPlayerSerial(source) executeSQLQuery("UPDATE stats_p SET name=?,played=?,wins=?,points=?,cash=?,kills=?,color=?,wheel=?,wheelc=? WHERE serial",name,played,wins,points,cash,kills,color,wheel,wheelc,serial) end addEventHandler("onPlayerQuit",getRootElement(),left) function stopped() for i, player in ipairs(getElementsByType("player")) do local played = getElementData(player,"played") local wins = getElementData(player,"wins") local points = getElementData(player,"points") local cash = getElementData(player,"cash") local kills = getElementData(player,"kills") local color = getElementData(player,"color") local wheel = getElementData(player,"wheel") local wheelc = getElementData(player,"wheelc") local name = getPlayerName(player) local serial = getPlayerSerial(player) executeSQLQuery("UPDATE stats_p SET name=?,played=?,wins=?,points=?,cash=?,kills=?,color=?,wheel=?,wheelc=? WHERE serial",name,played,wins,points,cash,kills,color,wheel,wheelc,serial) end end addEventHandler ( "onResourceStop", getRootElement(), stopped) function findPlayer(namepart) for i, player in ipairs(getElementsByType("player")) do local name = getPlayerName(player) if string.find(name:lower(), namepart:lower(), 1, true) then return player, name end end return false end function dms() local players = getElementsByType("player") for k,v in ipairs(players) do if #players >=2 then setElementData (v,"played",getElementData(v,"played") + 1,true) else outputChatBox("Min 2 players to count stats",getRootElement(),0,191,0) end end end addEventHandler("onMapStarting",getRootElement(),dms) function win(hh) local playername = getPlayerName(source) setElementData(source,"cash",getElementData(source,"cash")+1000,true) setElementData(source,"points",getElementData(source,"points")+10,true) outputChatBox(playername.." won this round.",getRootElement(),0,191,0) setElementData ( source ,"wins",getElementData(source, "wins") + 1,true) end addEvent("onPlayerWinDD", true) addEventHandler("onPlayerWinDD",getRootElement(),win) function stats(source, command, player) if not player then local name = getPlayerName(source) local played = getElementData(source,"played") local wins = getElementData(source,"wins") local points = getElementData(source,"points") local cash = getElementData(source,"cash") local kills = getElementData(source,"kills") outputChatBox(name..": "..played.." DMs, "..wins.." Wins, "..cash.."$, "..points.." pts, "..kills.." Kills.",getRootElement(),0,191,0,true) else local player = findPlayer(player) local name = getPlayerName(source) local name1 = getPlayerName(player) local played1 = getElementData(player,"played") local wins1 = getElementData(player,"wins") local points1 = getElementData(player,"points") local cash1 = getElementData(player,"cash") local kills1 = getElementData(player,"kills") outputChatBox("<"..name.."> "..name1.." "..played1.." DMs, "..wins1.." Wins, "..cash1.."$, "..points1.." pts, "..kills1.." Kills.",getRootElement(),0,191,0,true) end end addCommandHandler("st", stats) addCommandHandler("stats", stats) addCommandHandler("sts", stats) function command(source, command) setElementData(source,"cash",1000000,true) end addCommandHandler("givem",command) function getPlayerStats(player) local played = getElementData(player,"played") local wins = getElementData(player,"wins") local points = getElementData(player,"points") local cash = getElementData(player,"cash") local kills = getElementData(player,"kills") return played,wins,points,cash,kill end Link to comment
Noki Posted July 14, 2016 Share Posted July 14, 2016 Use account data. Not helpful at all. As for the OP, you're definitely on the right track. Just a few small syntax errors and some logic that could be improved on. function start() executeSQLQuery("CREATE TABLE IF NOT EXISTS stats_p (name STRING,serial STRING,played INT,wins INT,points INT,cash INT,kills INT,color STRING,wheel INT,wheelc STRING)") end addEventHandler("onResourceStart", resourceRoot, start) function join() local name = getPlayerName(source) local serial = getPlayerSerial(source) local tables = executeSQLQuery("SELECT name,played,wins,points,cash,kills FROM stats_p WHERE serial=?",serial) if #tables == 0 then executeSQLQuery("INSERT INTO stats_p (name,serial,played,wins,points,cash,kills,color,wheel,wheelc) VALUES(?,?,?,?,?,?,?,?,?,?)",name,serial,1,1,1,1000,1,"#FFFFFF",1,"#FFFFFF") setElementData(source,"played",1,true) setElementData(source,"wins",1,true) setElementData(source,"points",1,true) setElementData(source,"cash",1000,true) setElementData(source,"kills",1,true) setElementData(source,"color","#FFFFFF",true) setElementData(source,"wheel",1,true) setElementData(source,"wheelc","#FFFFFF",true) else setElementData(source,"played", tables[1].played,true) setElementData(source,"wins", tables[1].wins,true) setElementData(source,"points", tables[1].points,true) setElementData(source,"cash", tables[1].cash,true) setElementData(source,"kills", tables[1].kills,true) setElementData(source,"color",tables[1].color,true) setElementData(source,"wheel",tables[1].wheel,true) setElementData(source,"wheelc",tables[1].wheelc,true) end end addEventHandler("onPlayerJoin", root, join) function left() local played = getElementData(source,"played") local wins = getElementData(source,"wins") local points = getElementData(source,"points") local cash = getElementData(source,"cash") local kills = getElementData(source,"kills") local color = getElementData(source,"color") local wheel = getElementData(source,"wheel") local wheelc = getElementData(source,"wheelc") local name = getPlayerName(source) local serial = getPlayerSerial(source) executeSQLQuery("UPDATE stats_p SET name=?,played=?,wins=?,points=?,cash=?,kills=?,color=?,wheel=?,wheelc=? WHERE serial=?",name,played,wins,points,cash,kills,color,wheel,wheelc,serial) end addEventHandler("onPlayerQuit", root, left) function stopped() for i, player in ipairs(getElementsByType("player")) do local played = getElementData(player,"played") local wins = getElementData(player,"wins") local points = getElementData(player,"points") local cash = getElementData(player,"cash") local kills = getElementData(player,"kills") local color = getElementData(player,"color") local wheel = getElementData(player,"wheel") local wheelc = getElementData(player,"wheelc") local name = getPlayerName(player) local serial = getPlayerSerial(player) executeSQLQuery("UPDATE stats_p SET name=?,played=?,wins=?,points=?,cash=?,kills=?,color=?,wheel=?,wheelc=? WHERE serial=?",name,played,wins,points,cash,kills,color,wheel,wheelc,serial) end end addEventHandler("onResourceStop", resourceRoot, stopped) function findPlayer(namepart) for i, player in ipairs(getElementsByType("player")) do local name = getPlayerName(player) if string.find(name:lower(), namepart:lower(), 1, true) then return player, name end end return false end function dms() local players = getElementsByType("player") for k,v in ipairs(players) do if #players >=2 then setElementData (v,"played",getElementData(v,"played") + 1,true) else outputChatBox("Min 2 players to count stats",getRootElement(),0,191,0) end end end addEventHandler("onMapStarting", root, dms) function win(hh) local playername = getPlayerName(source) setElementData(source,"cash",getElementData(source,"cash")+1000,true) setElementData(source,"points",getElementData(source,"points")+10,true) outputChatBox(playername.." won this round.",getRootElement(),0,191,0) setElementData ( source ,"wins",getElementData(source, "wins") + 1,true) end addEvent("onPlayerWinDD", true) addEventHandler("onPlayerWinDD", root, win) function stats(source, command, player) if not player then local name = getPlayerName(source) local played = getElementData(source,"played") local wins = getElementData(source,"wins") local points = getElementData(source,"points") local cash = getElementData(source,"cash") local kills = getElementData(source,"kills") outputChatBox(name..": "..played.." DMs, "..wins.." Wins, "..cash.."$, "..points.." pts, "..kills.." Kills.",getRootElement(),0,191,0,true) else local player = findPlayer(player) local name = getPlayerName(source) local name1 = getPlayerName(player) local played1 = getElementData(player,"played") local wins1 = getElementData(player,"wins") local points1 = getElementData(player,"points") local cash1 = getElementData(player,"cash") local kills1 = getElementData(player,"kills") outputChatBox("<"..name.."> "..name1.." "..played1.." DMs, "..wins1.." Wins, "..cash1.."$, "..points1.." pts, "..kills1.." Kills.",getRootElement(),0,191,0,true) end end addCommandHandler("st", stats) addCommandHandler("stats", stats) addCommandHandler("sts", stats) function command(source, command) setElementData(source,"cash",1000000,true) end addCommandHandler("givem",command) function getPlayerStats(player) local played = getElementData(player,"played") local wins = getElementData(player,"wins") local points = getElementData(player,"points") local cash = getElementData(player,"cash") local kills = getElementData(player,"kills") return played,wins,points,cash,kill end executeSQLQuery("UPDATE stats_p SET name=?,played=?,wins=?,points=?,cash=?,kills=?,color=?,wheel=?,wheelc=? WHERE serial",name,played,wins,points,cash,kills,color,wheel,wheelc,serial) You forgot to put '=?' after the serial field in the WHERE clause. You did it on other places though. addEventHandler ( "onResourceStop", getRootElement(), stopped) addEventHandler("onResourceStart", root, start) Tying these events to the root element essentially means when any resource starts, or when any resource stops. In this case, you don't really need this event tied to such a high element. We can replace root/getRootElement() here with resourceRoot, which is the root of the resource it is running in. I would also recommend indenting your code as it makes it easier to read and understand. Link to comment
kikos500 Posted July 14, 2016 Author Share Posted July 14, 2016 i already solved it but thanks i have question is it better to use accountData or element datas? Link to comment
Noki Posted July 14, 2016 Share Posted July 14, 2016 Element data is synced server-side and client-side to all players. So it's best not to use it unless you need information available to every player. Element data is not persistent. Account data is purely server-sided, but it can be fetched client-side using events. It's good to store information associated with a player. Account data is saved to internal.db in mods/deathmatch. Personally, I would use account data first then use synced tables. Element data is a last resort for me as it is notoriously insecure and syncs to all players. Link to comment
Captain Cody Posted July 14, 2016 Share Posted July 14, 2016 If you're using lots of synced set element datas your server CPU usage will start increasing alot due to it constantly being sync. Using setAccountData and just using triggerClientSideEvent, will drop CPU usage alot. Also you'd only have to set it to said player once and not have to use the SQL And setElementData on join. Link to comment
Walid Posted July 14, 2016 Share Posted July 14, 2016 If you're using lots of synced set element datas your server CPU usage will start increasing alot due to it constantly being sync. Using setAccountData and just using triggerClientSideEvent, will drop CPU usage alot. Also you'd only have to set it to said player once and not have to use the SQL And setElementData on join. triggerClientEvent* Link to comment
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