The title character limit is too small to really explain what's happening here, so what I'm getting is this: I'm trying to make a model changing script, so that I can change my player model on the fly without using the model menu in the Q Menu, and overriding gamemode's functions with it. I'm trying to make it so you can change other player's models too, but that's for later, the core of it I want is to change my own model, regardless of what the gamemode set for it. Here's the code, help is much appreciated.
[lua]util.PrecacheModel( "models/player/odessa.mdl" )
util.PrecacheModel( "models/player/eli.mdl" )
util.PrecacheModel( "models/player/alyx.mdl" )
util.PrecacheModel( "models/player/barney.mdl" )
function ModelSwitcher(ply, cmd, args)
local models = { "models/player/odessa.mdl",
"models/player/eli.mdl",
"models/player/alyx.mdl",
"models/player/barney.mdl" }
local target = player.GetAll()
for k, v in pairs(target) do
if v:Nick(target).IsValid then
ply:SetModel(args[1])
else Msg("You don't exist ._.")
return end
end
end
function AutoCompleteOptions(cmdName, args)
return { "models/player/odessa.mdl", "models/player/eli.mdl", "models/player/alyx.mdl", "models/player/barney.mdl" }
end
concommand.Add("SetModel", AutoCompleteOptions, ModelSwitcher)
[/lua]
If I remember correctly, the auto complete function alway goes last, you just put your arguments to concommand.Add in the wrong order.
[lua]concommand.Add("SetModel", ModelSwitcher, AutoCompleteOptions)[/lua]
Also, next time mention the errors you're getting, that'll help people to find a solution to your problem faster.
That's the thing, there ARE NO errors! I think it's getting the code right, just not doing what it's supposed to. Suppose I'll put this here, this is what I want it to do:
1) Change the model.
Yes, that's it, that's all, no specific target finding or any of that crap, all I want it to do is target the person using it, and change their player model. The "error" I'm referring to is the spam from that Msg line, every character I put in it spams that string. I guess I could just tie the model to ply:SetModel() when you use the command, then screw around with it later.
What the flying hell, did you read the first part of my previous post? You swapped the functions around, and put the command as the auto complete function and the auto complete function as the command.
Sorry, was answering the second half of the post, I'll try that when I get home. Thanks for the help.
Alright, done a little bit of editing and fixed some attempt to index global "model" errors, and now it's putting up the message. Problem is, it's not reading from my table...not too sure what to do, as I've done as best I can from the example shown on the wiki. Is there any special trick that I have to use in order to make it change on the fly? Here's the code so far.
[lua]util.PrecacheModel( "models/player/odessa.mdl" )
util.PrecacheModel( "models/player/eli.mdl" )
util.PrecacheModel( "models/player/alyx.mdl" )
util.PrecacheModel( "models/player/barney.mdl" )
function ModelSwitcher(ply, cmd, args)
local models = { "models/player/odessa.mdl",
"models/player/eli.mdl",
"models/player/alyx.mdl",
"models/player/barney.mdl" }
if models.IsValid then
ply:SetModel(models)
elseif !models.IsValid then
Msg("this doesn't exist.")
end
end
function AutoCompleteOptions(cmdName, args)
return { "setmodel models/player/odessa.mdl", "setmodel models/player/eli.mdl", "setmodel models/player/alyx.mdl", "setmodel models/player/barney.mdl" }
end
concommand.Add("SetModel", ModelSwitcher, AutoCompleteOptions)
[/lua]
Oh, you're kinda new to Lua, aren't you?
There are many things which are horribly wrong in your code.
First of all, inside of ModelSwitcher, you are declaring "models" as a table, and then check if this table has a key called "IsValid". As a result, this statement will be always evaluated as false, because obviously, it does NOT exist. It would have been true if you declared "models" like this:
[lua]
local models = {
"models/player/odessa.mdl",
"models/player/eli.mdl",
"models/player/alyx.mdl",
"models/player/barney.mdl",
IsValid = true
}[/lua]
But it still makes absolutely no sense at all. You should go with the idea that a computer, and more generally, a programming language is completely retarded and will never guess what you want to do. IsValid is a function implemented in entities, and other various objects, to check if they exist or not. This will definitely not work on something as primitive as a table. Even if it worked, all you're doing is checking "if a table is valid". Which is completely pointless because you know you have declared that table, and it is obviously existing and valid.
Secondly, you're doing this:
[lua]ply:SetModel(models)[/lua]
As I already mentioned, Lua is dumb and cannot guess what you're talking about here. You're giving this function a list of models, how the hell are you expecting it to know which one to choose? Plus, GetModel expects a model, not a table.
[lua]
if models.IsValid then
...
elseif !models.IsValid then[/lua]
This is horrible and you should feel horrible for doing this. First of all, this is Lua, not C, so for god's sake, please forget about the unnatural syntax Garry introduced for the people who are too lazy to learn Lua, and write "not" instead of "!". "not" is Lua, "!" is C. You should know what language you are using, even though Garry made so "!" works, I still find it pretty retarded to mix up two different language like that.
I'm not blaming you, I'm blaming the people who wrote the examples you're basing yourself on.
Oh, and most importantly, if you have something like this:
[lua]if condition then
elseif not condition then[/lua]
This is completely pointless, as you can write this instead:
[lua]if condition then
else[/lua]
Finally, indent your damn code, this is unreadable. An if statement and its ending should be on the same column, same goes for functions, and eventually tables if you're writing them over several lines.
Here is some untested code, but it should work. It's commented and everything, so you understand how it's done exactly.
[lua]
-- Declare this table only once, this is much easier whenever you need to add more models to it
local models = {
"models/player/odessa.mdl",
"models/player/eli.mdl",
"models/player/alyx.mdl",
"models/player/barney.mdl"
}
--[[
This will create two tables based off the "models" table you have previously defined
modelIsValid assiocates the value "true" to every element of "models", this means modelIsValid["models/player/odessa.mdl"] = true, and so on
This means you will easily know whether a model is in this table without even having to code a search function
modelAutoComplete is just a duplicated of models, with "setmodel " added before every element
This also makes your job easier since you don't have to copy the whole models table yourself, all you have to do is return modelAutoComplete in the autocomplete function
]]
local modelIsValid = {}
local modelAutoComplete = {}
for k,v in ipairs(models) do
util.PrecacheModel(v)
modelIsValid[v] = true
modelAutoComplete[k] = "setmodel "..v
end
-- Switches models
function ModelSwitcher(ply, cmd, args)
if modelIsValid[args[1]] then
-- If the first argument is a model path which exists in the models table, then set the player's model to it
ply:SetModel(args[1])
else
-- Else, print a message to the console
-- Use MsgN instead of Msg, this will make sure a new line is added after your message. Else, everything will just keep being printed on the same line
MsgN("this doesn't exist.")
end
end
-- Autocomplete function
function AutoCompleteOptions(cmdName, args)
return modelAutoComplete
end
concommand.Add("SetModel", ModelSwitcher, AutoCompleteOptions)
[/lua]
Hope that helped.
wow.
Change the name of your autocomplete function or make it local. Otherwise there's a chance someone else has a global function called AutoCompleteOptions and they'll conflict.
[QUOTE=MegaJohnny;19763907]Change the name of your autocomplete function or make it local. Otherwise there's a chance someone else has a global function called AutoCompleteOptions and they'll conflict.[/QUOTE]
This, or make it local.
E: fml
[QUOTE=MegaJohnny;19763907]Change the name of your autocomplete function [b]or make it local.[/b][/QUOTE]
[QUOTE=Disseminate;19765869]This, or [b]make it local.[/b][/QUOTE]
Rated bad reading.
I am very new to Lua, so I'm just getting my bearings. Glad there's people around to help out :), thanks a lot for the new code, I'll try it out once I get home.
Sorry, you need to Log In to post a reply to this thread.