Jump to content

[Help] Tables


steadyfi

Recommended Posts

Hello :)

I have a little problem. It's my first time when I use tables and I wonder how can I make my script work.

This is what I do:

1. I loop through all of the accounts

2. Find the ones with the account data equal to what I need

3. Get the players name what is also stored as account data

4. Use table.insert to insert it into the table

(then loop again from 1 until there is nothing left)

5. And pass the table Client-Side where I need it

Client-Side Script:

addEvent("openteams.getPlayersFromAccounts.serverGave", true) 
addEventHandler("openteams.getPlayersFromAccounts.serverGave", getRootElement(), function(playerTable) 
    guiGridListClear(teamPlayersGrid) 
    for index, player in ipairs(playerTable) do 
        local row = guiGridListAddRow(teamPlayersGrid) 
        guiGridListSetItemText(teamPlayersGrid, row, 1, player, false, false) 
        getPlayerRow[players] = row 
    end 
end) 

I also have a trigger: (Client-Side too)

triggerServerEvent("openteams.getPlayersFromAccounts", localPlayer) 

Server-Side Script:

addEvent("openteams.getPlayersFromAccounts", true) 
addEventHandler("openteams.getPlayersFromAccounts", getRootElement(), function() 
    local team = getPlayerTeam(source) 
    if team then 
        --CODE 
            local playerTable = {} 
            local allAccounts = getAccounts() 
            for _, account in ipairs(getAccounts()) do 
                if getTeamName(team) == getAccountData(account, "openteams.team.name") then 
                    local playerName = getAccountData(account, "openteams.account.name") 
                    table.insert(playerTable, playerName) 
                end 
            end 
        --END CODE 
        triggerClientEvent(source, "openteams.getPlayersFromAccounts.serverGave", source, playerTable) 
    end 
end) 

Hope you understand what I want to do, i tried my best to explain.

Thank you :D

Link to comment

EDIT:(not tested).

--- SERVER 
addEvent("openteams.getPlayersFromAccounts", true) 
addEventHandler("openteams.getPlayersFromAccounts", getRootElement(), function() 
    local team = getPlayerTeam(source) 
    if team then 
        --CODE 
            local allAccounts = getAccounts() 
            for _, account in ipairs(getAccounts()) do 
                if getTeamName(team) == getAccountData(account, "openteams.team.name") then 
                    local playerName = getAccountData(account, "openteams.account.name") 
                    triggerClientEvent(source, "openteams.getPlayersFromAccounts.serverGave", source, playerName)                    
  
                end 
            end 
        --END CODE 
  
    end 
end) 
  
---CLIENT 
local playerTable = {} 
  
addEvent("openteams.getPlayersFromAccounts.serverGave", true) 
addEventHandler("openteams.getPlayersFromAccounts.serverGave", getRootElement(),  
function(pName) 
    guiGridListClear(teamPlayersGrid) 
    table.insert(playerTable, {pName}) 
    for index, player in ipairs(playerTable) do 
        local row = guiGridListAddRow(teamPlayersGrid) 
        guiGridListSetItemText(teamPlayersGrid, row, 1, player, false, false) 
        getPlayerRow[players] = row 
    end 
end) 

Link to comment
EDIT:(not tested).
--- SERVER 
addEvent("openteams.getPlayersFromAccounts", true) 
addEventHandler("openteams.getPlayersFromAccounts", getRootElement(), function() 
    local team = getPlayerTeam(source) 
    if team then 
        --CODE 
            local allAccounts = getAccounts() 
            for _, account in ipairs(getAccounts()) do 
                if getTeamName(team) == getAccountData(account, "openteams.team.name") then 
                    local playerName = getAccountData(account, "openteams.account.name") 
                    triggerClientEvent(source, "openteams.getPlayersFromAccounts.serverGave", source, playerName)                    
  
                end 
            end 
        --END CODE 
  
    end 
end) 
  
---CLIENT 
local playerTable = {} 
  
addEvent("openteams.getPlayersFromAccounts.serverGave", true) 
addEventHandler("openteams.getPlayersFromAccounts.serverGave", getRootElement(),  
function(pName) 
    guiGridListClear(teamPlayersGrid) 
    table.insert(playerTable, {pName}) 
    for index, player in ipairs(playerTable) do 
        local row = guiGridListAddRow(teamPlayersGrid) 
        guiGridListSetItemText(teamPlayersGrid, row, 1, player, false, false) 
        getPlayerRow[players] = row 
    end 
end) 

Didn't work.

Error:

HukkiAa.png

Link to comment
Why you dont pass the elements that you need in players table to client side then insert them there

I kinda imagine what you are talking about but i'm not sure. Could you please make it for me ? Like i said, it's the first time I use tables so I don't know anything about them.

Link to comment
  
  
        guiGridListSetItemText(teamPlayersGrid, row, 1, player[index], false, false) 
  
  

replace that line (247) with this one

of course this problem happen because player still a table and to access player namz you should put the table index

Link to comment
  
  
        guiGridListSetItemText(teamPlayersGrid, row, 1, player[index], false, false) 
  
  

replace that line (247) with this one

of course this problem happen because player still a table and to access player namz you should put the table index

It's working, but it still gives me an error:

expected string at argument 4, got nil

Link to comment
Ok hmmm post the new code

Client-Side:

local playerTable = {} 
  
addEvent("openteams.getPlayersFromAccounts.serverGave", true) 
addEventHandler("openteams.getPlayersFromAccounts.serverGave", getRootElement(), 
function(pName) 
    guiGridListClear(teamPlayersGrid) 
    table.insert(playerTable, {pName}) 
    for index, player in ipairs(playerTable) do 
        local row = guiGridListAddRow(teamPlayersGrid) 
        guiGridListSetItemText(teamPlayersGrid, row, 1, player[index], false, false) 
        getPlayerRow[player] = row 
    end 
end) 

The getPlayerRow = {} is also added, it's just not in this part of the file

Server-Side:

addEvent("openteams.getPlayersFromAccounts", true) 
addEventHandler("openteams.getPlayersFromAccounts", getRootElement(), function() 
    local team = getPlayerTeam(source) 
    if team then 
        --CODE 
            local allAccounts = getAccounts() 
            for _, account in ipairs(getAccounts()) do 
                if getTeamName(team) == getAccountData(account, "openteams.team.name") then 
                    local playerName = getAccountData(account, "openteams.account.name") 
                    triggerClientEvent(source, "openteams.getPlayersFromAccounts.serverGave", source, playerName)                   
                end 
            end 
        --END CODE 
    end 
end) 

Link to comment
Oh my god its my mistake replace player[index] to player[1]

Ok, but it now duplicates everytime I run the function.

The gridlist doesn't get cleared

EDIT: Nevermind, I just added the table in my gui function so everytime when I open it the table gets reseted

Edited by Guest
Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...