Not wanting to learn Lua in depth, just want to get a random number of traitors in TTT for my friends and I to use.
Code:
hook.Add("TTTEndRound", "tttrandom.roles", function()
RunConsoleCommand("ttt_traitor_pct", math.random(0,1))
print("Randomizing T's ")
end)
Running via console command in Gmod:
lua_openscript randomtraitors.lua
randomtraitors.lua being the file name.
I get this error:
] lua_openscript randomtraitors.lua
Running script randomtraitors.lua...
[ERROR] lua/randomtraitors.lua:1: '=' expected near '<eof>'
1. unknown - lua/randomtraitors.lua:0
Anybody know a fix?
You have an extra parentheses at the end of the RunConsoleCommand line that is mucking things up, the actual error you're getting is a red herring.
That is literally all the code there is. I can't get it to run in Gmod as I get the '=' expected near '<eof>' error every time I run it. Am I missing some code that allows me to run the lua?
Remove the '()' after the keyword 'function' inside hook.Add.
RunConsoleCommand indicates otherwise.
@OP are you entirely sure that the code you're showing is what's actually saved to the script your running? I know that seems like a weird question but unless I'm missing something Gmod specific that seems like perfectly valid Lua.
Like is it possible that you're saving as isn't saving to the right place?
Nope, same error.
Also just in case its my directory, its saved in
D:\Steam\steamapps\common\GarrysMod\garrysmod\lua
I'd seen some things suggesting to put it in D:\Steam\steamapps\common\GarrysMod\garrysmod\lua\autorun or D:\Steam\steamapps\common\GarrysMod\garrysmod\lua\autorun\server but I can't get Gmod to even detect those.
and changing the encoding to unicode rather than ANSI (this is from a guide I saw).
Never had to do that. I use Notepad++ and export with default settings, and my code always works. Also, you put it in lua/autorun if you want it to start as soon as you initialize. Also, try putting it in garrysmod/addons/<addon name>/lua
Uninstalled Gmod and moved it to my C drive, repeated and left it on ANSI and the script ran. Thanks for your help.
While I'm on this thread, at the end of every round now the server puts the message in chat saying what the pct had been changed to. Is there anyway to stop this, so people will have no idea how many traitors there are?
The ttt_traitor_pct ConVar probably has the FCVAR_NOTIFY flag, you can probably try changing it where the ConVar is defined.
I appreciate the help, but I know nothing about this, I literally just want to set up a TTT with my friends. Explain like I'm 5?
Somewhere in the code, there is the ConVar defined using CreateConVar, and has specific flags set on it, one of them being a flag that notifies whenever that ConVar's value changes. You will need to find that part of code and edit it so that the flag is not set.
Okay so having done a bit of testing with this, the new code is:
hook.Add("TTTEndRound", "tttrandom.roles", function()
RunConsoleCommand("ttt_traitor_pct", math.random())
print("Randomizing T's ")
end)
This works. Ignoring the message in chat, this works almost perfectly. The issue I now have is defining the parameters (correct term?) of math.random
Leaving it as math.random() randomly selects between 0.00 and 1.00, which does work. From what I have seen, I should be able to modify this to math.random(0.1,0.5) and the values will be randomly between 0.10 and 0.50. This is not the case however. Running the script, for the first round ALWAYS selects 0.10, the next round then being set to 1.10. Then back to 0.1, back to 1.1, and this continues.
Am I doing something wrong with math.random? Any help is appreciated.
What? Just use math.random alongside player.GetCount
So something like
math.random(1, math.Round(player.GetCount() / .5))
So the traitors would be between 1 and 50% of the player count. The math.Round is so the number doesnt have a decimal.
This works, but TTT works on a percentage. So 1 is 100% of the players on the server, 0.5 is 50% of the players on the server. So if player.GetCount gets a value of 7, TTT/Gmod interprets this as 700% of players need to be traitors, which then cause it to crash.
Sorry, you need to Log In to post a reply to this thread.