How to make a RNG that assigns a player as a particular playermodel?

Okay, this is probably my weirdest and most hard task for someone to program.

Brief notice: I know nothing about lua coding apart from the small times I’ve code monkeyed. So, if you do explain lua, please treat me like a child that was dropped as a baby nonstop since the day they were born.

Background information: So, on my server I got a playermodel for an African Penguin. Fairly simple, right? I have it in my dedicated server in the addons folder, and it is in my pointshop which has no mods to it besides a subcategory mod that doesn’t really change much.
This African Penguin playermodel is really small and so we decided to call it OP, as virtually nobody can hit it. I had many suggestions and urged to get rid of it, or better yet, adjust its size, however my players decided against it.

Question: So, that’s the background to me weird question, and this is: How can I randomly select someone who is currently playing the game, and then give that playermodel to them?

Needed Information: So I understand this will count as an addon, I guess, and so nobody will really help as it’s frankly a waste of time, however I understand that it’ll need a Random Number Generator (RNG) and then it’d give a command that’d then give them that playermodel. I assume it would be kind of simple, like just select someone at random using that RNG, and then run a command to give my playermodel (the model of which is located in garrysmod/addons/African Penguin/tsbb/animals/african_penguin.mdl) using a command to give them the model.

Instructions as to what I need and how I think I do it: As someone who can’t do basic lua, I don’t know how to do this, so I installed Bite My Apple’s Addon which can be found at https://forums.ulyssesmod.net/index.php/topic,6184 which works as a command of !setmodel [name] [model]. So, could you help me write a basic lua file that would randomly select a player, fetch their name, and then run “ulx setmodel [fetched name] models/tsbb/animals/african_penguin.mdl” ?

What I need: So basically, what I want to know is how to randomly select someone and then fetch their name, and then use that name in a command, and then run that command in the server console as an addon to my server that plays every round.

Optional help: HOWEVER, I would also like limitations to this. Would it be possible to limit it so that this command is only ran if there are over 6 players on the server, and it cannot happen to the same person twice in a row? Could I also make it run an announcement like csay throughout the server, announcing that the fetched name has become the penguin?

Thanks so much for anybody that would dare help me! I know this is tricky and likely a waste of time, but thank you even if you just wanted to say hi or wanted to have a look.

It’s not very difficult. You just need

math.random, or

table.Random.



RunConsoleCommand( "ulx setmodel", table.Random( player.GetAll() ):Nick() .. " models/tsbb/animals/african_penguin.mdl" )


^ This is untested

[editline]19th November 2017[/editline]

If you want it to run only with more than 6 players with no repeats then this might work, but this code’s pretty bad:



local old = 0
local count = player.GetCount()
if count > 6 then
	local new = math.random( count )
	while new == old do
		new = math.random( count )
	end
	RunConsoleCommand( "ulx setmodel", player.GetAll()[ new ]:Nick() .. " models/tsbb/animals/african_penguin.mdl" )
	old = new
end


I don’t know a good way to generate a different random number every time, I’m sure someone else will

Wow, I was not expecting such a fast, good, and detailed reply so quickly! Thank you ever so much! I haven’t tested this, so I can’t say if it works or not, but either way: thank you.

Perhaps I should elaborate a little bit: This code is all very good, but I don’t think I specified certain things because I’m an idiot. The server I use uses the gamemode TTT (Not sure if that’s important or relevant in the slightest, but too much information > too little information) and I would like this code to run every single time a round starts, and after the preparation stage (so at the same time as the roles are assigned and the ‘proper’ game starts.

If you can’t help any further with my plea, that’s fine. Or, if the answer is surprisingly simple and you think I’m dumb as bricks, that’s fine as well. Either way, thanks so much for your help. It means a lot :slight_smile:

I think TTTBeginRound might work for that



local old = 0
hook.Add( "TTTBeginRound", "blah", function()
	local count = player.GetCount()
	if count > 6 then
		local new = math.random( count )
		while new == old do
			new = math.random( count )
		end
		RunConsoleCommand( "ulx setmodel", player.GetAll()[ new ]:Nick() .. " models/tsbb/animals/african_penguin.mdl" )
		old = new
	end
end )


I haven’t tested any of this so it’s probably useless crap