• Using tables correctly.
    2 replies, posted
I want to learn coding so I can make some amazing things that people on facepunch have made. I'm running into an issue. I have a basic understanding on using tables. I had a friend help me (after some people from facepunch helped) make my script efficient with the use of tables. I'm trying to make it so if a steamid matches with the player their playermodel and color are set to the one in the table. How would I detect/set if all of them match, then set them to the correct model/color. I've tried and I'll post what I have so far. Sorry if it's a stupid mistake I'm doing. [lua]local mim = { ["STEAM_0:1:00000"] = { ["models/player/gman_high.mdl"] = { [Color(0,0,0)] = true } } } local function SetPlayerModelsBSID(ply) if not mim[ply:SteamID()] then return end if mim[ply:SteamID()] then end end hook.Add("PlayerSpawn","SetPlayerModelsBSID", SetPlayerModelsBSID )[/lua] How would I correctly make this type of table/script? I'd love any suggestions/criticism.
You're using tables fine, but it's a bit difficult to set models/player colors the way you're doing it. I'd probably redo the table a little so it'd be more like this: [CODE] local mim = { ["STEAM_0:1:00000"] = { model = "models/player/gman_high.mdl", color = Color(0,0,0) } } [/CODE] All I did to your table was to make both of the things (the model and the color) to be values in the table, rather than one being a key and one a value. Then, you can simply use the keys set as those values there by doing something like this: [CODE] local id = ply:SteamID() if mim[ id ] then ply:SetModel( mim[ id ].model ) -- doing table.something get the key 'something' by assuming the key is a string ply:SetColor( mim[ id ].color ) -- same with this, but instead of using SetColor, use SetPlayerColor with a vector instead (colors can be converted to vectors using the example on the wiki page) end [/CODE]
[QUOTE=MPan1;50311369]You're using tables fine, but it's a bit difficult to set models/player colors the way you're doing it. I'd probably redo the table a little so it'd be more like this: [CODE] local mim = { ["STEAM_0:1:00000"] = { model = "models/player/gman_high.mdl", color = Color(0,0,0) } } [/CODE] All I did to your table was to make both of the things (the model and the color) to be values in the table, rather than one being a key and one a value. Then, you can simply use the keys set as those values there by doing something like this: [CODE] local id = ply:SteamID() if mim[ id ] then ply:SetModel( mim[ id ].model ) -- doing table.something get the key 'something' by assuming the key is a string ply:SetColor( mim[ id ].color ) -- same with this, but instead of using SetColor, use SetPlayerColor with a vector instead (colors can be converted to vectors using the example on the wiki page) end [/CODE][/QUOTE] Thank you, I got it working. [lua]local function SetPlayerModelsBSID(ply) if M.Config.EnableCertainPlayerModels then local id = ply:SteamID() if not M.Players[id] then return end ply:SetModel(M.Players[id].model) return true end end hook.Add("PlayerSetModel","SetPlayerModelsBSID", SetPlayerModelsBSID ) local function SetPlayerColorsBSID(ply) if M.Config.EnableCertainPlayerColors then local id = ply:SteamID() if not M.Players[id] then return end local c = M.Players[id].color ply:SetPlayerColor(Vector(c.r / 255, c.g / 255, c.b / 255)) return true end end hook.Add("TTTPlayerSetColor", "SetPlayerColorsBSID", SetPlayerColorsBSID)[/lua]
Sorry, you need to Log In to post a reply to this thread.