Firespider Posted May 5, 2023 Share Posted May 5, 2023 Hello, I'm pretty stupid, I wrote a script for the databases that saves the name, serail, food and money, but for some reason it's not good. local conn = dbConnect("mysql", "dbname=test; host=127.0.0.1;charset=utf8", "root","","share=0") function SQL(hunger) local qh = dbQuery(conn,"SELECT * FROM playeradatok WHERE serial=?",getPlayerSerial(source)) local res = dbPoll(qh, 500) if #res > 0 then for _,rows in pairs(res) do setPlayerMoney(source,rows["money"]) setElementData(player, "hunger") end else local playername = getPlayerName(source) local money = getPlayerMoney(source) local serial = getPlayerSerial(source) local hunger = getElementData(player, "hunger") dbExec(conn,"INSERT into playeradatok (playername, serial, money, hunger) VALUES (?,?,?,?, NOW())", username, serial, money, hunger) end end function saveHunger(hunger) -- Elmentjük a változó értékét a játékos adataiba setElementData(source, "hunger", hunger) end addEvent("onClientSaveHunger", true) addEventHandler("onClientSaveHunger", root, saveHunger) function getHunger(player) local hunger = getElementData(player, "hunger") -- itt használd fel a hunger változó értékét end Link to comment
Mvrat Posted May 5, 2023 Share Posted May 5, 2023 in setElementData and getElementData functions you're using the "player" variable which is not defined in the code. In the for loop where you set the element data for the player's hunger you not specified the value that has to be set. Im cleaned your code a little bit. Try it, please. Hope it works. local db = dbConnect("mysql", "dbname=test; host=127.0.0.1;charset=utf8", "root", "", "share=0") -- This function loads the player's data from the database function loadPlayerData(player) -- Load the player's data from the database local query = dbQuery(db, "SELECT * FROM players WHERE serial = ?", getPlayerSerial(player)) local result = dbPoll(query, -1) -- If loading the data failed, use default values if not result or #result == 0 then setElementData(player, "money", getPlayerMoney(player)) setElementData(player, "hunger", 100) else setElementData(player, "money", result[1].money) setElementData(player, "hunger", result[1].hunger) end end -- This function saves the player's data to the database function savePlayerData(player) local money = getPlayerMoney(player) local hunger = getElementData(player, "hunger") -- Save the player's data to the database local query = dbQuery(db, "REPLACE INTO players (serial, money, hunger) VALUES (?, ?, ?)", getPlayerSerial(player), money, hunger) dbFree(query) end -- Function to return the player's hunger value function getPlayerHunger(player) return getElementData(player, "hunger") end -- Load the player's data when they join the server addEventHandler("onPlayerJoin", root, function() loadPlayerData(source) end) -- Save the player's data when they quit the server addEventHandler("onPlayerQuit", root, function() savePlayerData(source) end) -- Save the player's data when a parameter is changed addEventHandler("onElementDataChange", root, function(dataName) if dataName == "money" or dataName == "hunger" then savePlayerData(source) end end) 1 Link to comment
Firespider Posted May 6, 2023 Author Share Posted May 6, 2023 @Mvrat doesn't working. Link to comment
Mvrat Posted May 9, 2023 Share Posted May 9, 2023 Can you post a screenshot of the error, warning messages and database table structure? 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