beamer Posted May 23, 2022 Share Posted May 23, 2022 good afternoon! help me please! when a player leaves the server, the team in which he was a member remains in the tab how to make it so that when ALL team members exit, its name is removed from the tab? below is a snippet of code that might help you local inPlayerTeam = {} addEventHandler("onResourceStart", getResourceRootElement(getThisResource()), function() executeSQLQuery("CREATE TABLE IF NOT EXISTS tab_team(name TEXT, login TEXT, level NUMERIC, r NUMERIC, g NUMERIC, b NUMERIC)") local res = executeSQLQuery("SELECT * FROM tab_team WHERE level = ?", 1) if #res > 0 then for _, t in ipairs(res) do createTeam(t.name, t.r, t.g, t.b) end end outputDebugString("Создано "..(#res).." групп") end ) addEventHandler('onClientRender', root, function() for i, team in ipairs(getElementsByType('team')) do local players = getPlayersInTeam(team) if (#players == 0) then destroyElement(team) end end end) addEventHandler("onResourceStop", getResourceRootElement(getThisResource()), function() for _, t in ipairs(getElementsByType("team")) do destroyElement(t) end outputDebugString("Все группы удалены") end ) addEventHandler("onPlayerLogin", getRootElement(), function(_, account) local login = getAccountName(account) setElementData(source, "tab_team:login", login) local res = executeSQLQuery("SELECT name FROM tab_team WHERE login = ?", login) if #res > 0 then setPlayerTeam(source, getTeamFromName(res[1].name)) end end ) addEventHandler("onPlayerQuit", getRootElement(), function() if inPlayerTeam[source] then inPlayerTeam[source] = destroyElement(team) end end ) Link to comment
Platin Posted May 23, 2022 Share Posted May 23, 2022 (edited) Hi, let's start by defining where you should do this. I see that you have added an `onClientRender` on a server-side script file. You can't do this, because `onClientRender` will only work client-side, and will trigger every frame. You should also try to avoid using `onClientRender` to calculate this type of thing, that event is useful for rendering stuff or trying to simulate things, but because it triggers every frame it can be bad for performance. What you want to do is actually pretty simple: local listTeams = {} -- This will have all the teams, key = teamName and value = team, this will be in place in order to avoid calling 'getTeamFromName' and also will be useful to keep track of which teams there are local inPlayerTeam = {} addEventHandler("onPlayerLogin", root, function(_, account) local login = getAccountName(account) setElementData(source, "tab_team:login", login) local res = executeSQLQuery("SELECT name FROM tab_team WHERE login = ?;", login) if #res > 0 then -- This will create the teams on demand if not listTeams[res[1].name] then listTeams[res[1].name] = createTeam(res[1].name, res[1].r, res[1].g, res[1].b) end setPlayerTeam(source, listTeams[res[1].name]) inPlayerTeam[source] = listTeams[res[1].name] end end ) addEventHandler("onPlayerQuit", root, function() if inPlayerTeam[source] then -- This will make the function stop executing if there is another player other than the one that just quitted for k,player in ipairs(getPlayersInTeam(inPlayerTeam[source])) do if player ~= source then return end end -- If there isn't, then we will destroy the team and we will free the memory in the table listTeams[getTeamName(inPlayerTeam[source])] = nil destroyElement(inPlayerTeam[source]) inPlayerTeam[source] = nil end end ) The teams will remove themselfs on their own if the resource stops. This should create the teams when the player logs in and they should be destroyed when the player quits. If you want a simpler version you can make this script with absolutely NO tables (except `getPlayersInTeam` and your query result) using `getPlayerTeam` and `getTeamFromName`. Didn't test it, but it should work just fine. Edited May 23, 2022 by Platin Forgot to free memory of listTeams Link to comment
beamer Posted May 24, 2022 Author Share Posted May 24, 2022 (edited) hm Edited May 24, 2022 by beamer Link to comment
beamer Posted May 24, 2022 Author Share Posted May 24, 2022 @Platin the command when the player exits still remains in the tab and when a player login on - he is not added to the team in the tab Link to comment
beamer Posted May 24, 2022 Author Share Posted May 24, 2022 (edited) its done, i fixed it. thx @Platin Edited May 24, 2022 by beamer 1 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