Hello, I haven't had to experiment with tables in quite some time but I was just wondering how I could do this.
So I have a table of player-models that looks a little like this:
[code]
local ayylmao = {
Model("models/player/ply1.mdl"),
Model("models/player/ply2.mdl"),
Model("models/player/ply3.mdl"),
Model("models/player/ply4.mdl")
};
[/code]
I was just wondering what I could use to return the table with table.Random without repeating one.
(ex: randomly getting ply3, nobody else can get ply3. etc)
Hopefully I'm explaining this correctly.
[code]
local ayylmao = {
Model("models/player/ply1.mdl"),
Model("models/player/ply2.mdl"),
Model("models/player/ply3.mdl"),
Model("models/player/ply4.mdl")
};
local availablemodels = ayylmao;
-- when a player gets their model selected
local mdl = table.Random(availablemodels);
ply:SetModel(mdl);
table.RemoveByValue(availablemodels, mdl); -- not sure if i'm using this right, but i think that's how it would go
[/code]
[QUOTE=RonanZer0;47934508][code]
local ayylmao = {
Model("models/player/ply1.mdl"),
Model("models/player/ply2.mdl"),
Model("models/player/ply3.mdl"),
Model("models/player/ply4.mdl")
};
local availablemodels = ayylmao;
-- when a player gets their model selected
local mdl = table.Random(availablemodels);
ply:SetModel(mdl);
table.RemoveByValue(availablemodels, mdl); -- not sure if i'm using this right, but i think that's how it would go
[/code][/QUOTE]
Easier and more efficient way to do it is:
[code]
local ayylmao = {
Model("models/player/ply1.mdl"),
Model("models/player/ply2.mdl"),
Model("models/player/ply3.mdl"),
Model("models/player/ply4.mdl")
}
local rind = math.random(1, #ayylmao)
local val = ayylmao[rind]
ayylmao[rind] = nil
[/code]
It'll save you 2 iterations of the table (1 for table.Random, one for table.RemoveByValue).
so remove from the table itself?
what if he wants to reset them later?
[QUOTE=RonanZer0;47934560]so remove from the table itself?
what if he wants to reset them later?[/QUOTE]
Obviously he can do what you did. You also need to copy the table because tables aren't copied by putting them in a local variable.
[code]
local ayylmao = {
Model("models/player/ply1.mdl"),
Model("models/player/ply2.mdl"),
Model("models/player/ply3.mdl"),
Model("models/player/ply4.mdl")
}
local modeltab = table.Copy(ayylmao)
local rind = math.random(1, #modeltab)
local val = modeltab[rind]
modeltab[rind] = nil
[/code]
[QUOTE=RonanZer0;47934508][code]
local ayylmao = {
Model("models/player/ply1.mdl"),
Model("models/player/ply2.mdl"),
Model("models/player/ply3.mdl"),
Model("models/player/ply4.mdl")
};
local availablemodels = ayylmao;
-- when a player gets their model selected
local mdl = table.Random(availablemodels);
ply:SetModel(mdl);
table.RemoveByValue(availablemodels, mdl); -- not sure if i'm using this right, but i think that's how it would go
[/code][/QUOTE]
Oddly enough, this didn't seem to work correctly. It seemed to only set/use the last model in the table.
I've tried the other method aswell, but I couldn't get the models to even appear correctly using this:
[code]
local ayylmao = {
Model("models/player/ply1.mdl"),
Model("models/player/ply2.mdl"),
Model("models/player/ply3.mdl"),
Model("models/player/ply4.mdl")
}
local rind = math.random(1, #ayylmao)
local val = ayylmao[rind]
ayylmao[rind] = nil
ply:SetModel(val)
[/code]
(Also tried replacing val with rind for the SetModel part but didn't seem to work)
[QUOTE=kpjVideo;47934630]Oddly enough, this didn't seem to work correctly. It seemed to only set/use the last model in the table.
I've tried the other method aswell, but I couldn't get the models to even appear correctly using this:
[code]
local ayylmao = {
Model("models/player/ply1.mdl"),
Model("models/player/ply2.mdl"),
Model("models/player/ply3.mdl"),
Model("models/player/ply4.mdl")
}
local rind = math.random(1, #ayylmao)
local val = ayylmao[rind]
ayylmao[rind] = nil
ply:SetModel(val)
[/code]
(Also tried replacing val with rind for the SetModel part but didn't seem to work)[/QUOTE]
My code works for me. It will work for the first three times and does nothing after that.
[code]
local ayylmao = { -- These aren't your models, I just took them from the valid model list.
"models/player/Group03/female_02.mdl",
"models/player/Group01/female_05.mdl",
"models/player/guerilla.mdl"
}
local modeltab = table.Copy(ayylmao)
local rind = math.random(1, #modeltab)
ply:SetModel(modeltab[rind])
table.remove(modeltab, rind) -- Forgot to add this; it shifts the values down
[/code]
Ah, I see the issue I forgot to remove the precaching of the models before setting them, seems to be working with this now
[code]
local ayylmao = {
Model("models/player/ply1.mdl"),
Model("models/player/ply2.mdl"),
Model("models/player/ply3.mdl"),
Model("models/player/ply4.mdl")
}
local rind = math.random(1, #ayylmao)
local val = ayylmao[rind]
ayylmao[rind] = nil
ply:SetModel(val)
[/code]
Thanks everyone! <3<3
Sorry, you need to Log In to post a reply to this thread.