• [B]Last Prop Standing script[/B]
    30 replies, posted
I'm looking for a code for prop hunt that gives the last prop a weapon. I've tried creating my own but nothing works. I've only seen a few servers that had this feature, so finding a code is next to impossible. Here is a code that might be helpful, its the class_hunter.lua [lua]// Called by spawn and sets loadout function CLASS:Loadout(pl) pl:Give("weapon_crowbar") pl:GiveAmmo(64, "Buckshot") pl:GiveAmmo(255, "SMG1") pl:Give("weapon_shotgun") pl:Give("weapon_smg1") if GetConVar("WEAPONS_ALLOW_GRENADE"):GetBool() then pl:Give("item_ar2_grenade") end local cl_defaultweapon = pl:GetInfo("cl_defaultweapon") if pl:HasWeapon(cl_defaultweapon) then pl:SelectWeapon(cl_defaultweapon) end end[/lua] and here is class_prop.lua [lua]// Called by spawn and sets loadout function CLASS:Loadout(pl) // Props dont get weapons. end[/lua] Since props don't gets weapons by default, I'm sure there is some other editing that needs to be done to make it work. Any help with this would be greatly appreciated.
on player death hook if num living props == 1 give him wep that's the pseudo kode, good luck
Still can't figure it out. Only death call I have is this [lua]function GM:CheckPlayerDeathRoundEnd() if !GAMEMODE.RoundBased || !GAMEMODE:InRound() then return end local Teams = GAMEMODE:GetTeamAliveCounts() if table.Count(Teams) == 0 then GAMEMODE:RoundEndWithResult(1001, "Draw, everyone loses!") return end if table.Count(Teams) == 1 then local TeamID = table.GetFirstKey(Teams) GAMEMODE:RoundEndWithResult(TeamID, team.GetName(TeamID).." win!") return end end[/lua] I've tried many different things but got nothing. I'm kinda new to lua so I'm probably missing something simple. Anyway, I'd appreciate a little more details on this.
[url]http://wiki.garrysmod.com/page/GM/PlayerDeath[/url]
[QUOTE=Tomelyr;46045944][url]http://wiki.garrysmod.com/page/GM/PlayerDeath[/url][/QUOTE] The function GM:PlayerDeath isn't what I'm having trouble with, its what comes after it.
[lua]function CheckLastProp() local prop_alive = {} for k,ply in pairs( team.GetPlayers(TEAM_PROP) ) do if ply:Alive() then table.insert( prop_alive, ply ) end end if #prop_alive == 1 then prop_alive[1]:Give("weapon_shotgun") end end hook.Add("PlayerDeath", "KillOneGetGun", CheckLastProp()) [/lua] Pseudocode, didn't tested it and could be totaly wrong
[QUOTE=Tomelyr;46046783][lua]function CheckLastProp() local prop_alive = {} for k,ply in pairs( team.GetPlayers(TEAM_PROP) ) do if ply:Alive() then table.insert( prop_alive, ply ) end end if #prop_alive == 1 then prop_alive[1]:Give("weapon_shotgun") end end hook.Add("PlayerDeath", "KillOneGetGun", CheckLastProp()) [/lua] Pseudocode, didn't tested it and could be totaly wrong[/QUOTE] Nah doesnt work. I edited it around but still nothing. Thanks for trying to help though.
maybe linkin the prop hunt version would help. also what did u edit speficly?
I know the prop hunt version I work with, the PlayerDeath hook doesn't get called for props, because they are silently killed on death.
[QUOTE=Tomelyr;46047078]maybe linkin the prop hunt version would help. also what did u edit speficly?[/QUOTE] I don't remember what I tried changing. But I am pretty sure its TEAM_PROPS, not PROP Either way, I dont know much about lua/pseudo. I've just customized some basic codes is all. This one is kicking my ass though cause I want it but cant figure it out. and what do you mean linkin the prop hunt version?
the link. as Blasteh mentioned, you could try PlayerSilentDeath insteed of PlayerDeat in the hook.Add
Nah, still nothing.
Alright so I still cant get this one working. I've got it down to this. [lua] function CheckLastProp() local prop_alive = {} for k,ply in pairs( team.GetPlayers(TEAM_PROPS) ) do if ply:Alive() then table.insert( prop_alive, ply ) end end if #prop_alive == "1" then prop_alive[1]:Give("weapon_shotgun") end end hook.Add("CheckPlayerDeathRoundEnd", "KillOneGetGun", CheckLastProp())[/lua] Please help if you know what the problem is. oh and I think CheckPlayerDeathRoundEnd is the only one prop hunt uses for deaths. Anyway, please help me figure this out.
PlayerDeath should still be called... But, because I don't know much of anything at all about prop-hunt, consider this pseudo-code, albeit should work. If you know how many "props" you have at round start, save the number somewhere. On game-event player_disconnect, if player was prop, subtract 1 from our number. On hook PlayerDeath, if victim was prop, subtract 1 from our number. Then, your CheckPlayerDeathRoundEnd would only need to see if the number is !( x > 0 ) or you can use ( x < 1 ) for round end; == 1 for your shotgun... If you want to easily be able to target the last prop without going through a list, do this.... On round start, grab all valid props. Add them to a table. _table[ SteamID ] = PlayerObject; On hook PlayerDeath, or game-event player_disconnect, if _table[ SteamID ] then _table[ SteamID ] = nil; I'd also recommend having a counter variable too; saves time on table.Count... So subtract 1 in that case too.. If counter == 1 then _table[ table.GetFirstKey( _table ) ] is our player object... Give that prop the weapon.
[QUOTE=covey88;46064550]Alright so I still cant get this one working. I've got it down to this. [lua] function CheckLastProp() local prop_alive = {} for k,ply in pairs( team.GetPlayers(TEAM_PROPS) ) do if ply:Alive() then table.insert( prop_alive, ply ) end end if #prop_alive == "1" then prop_alive[1]:Give("weapon_shotgun") end end hook.Add("CheckPlayerDeathRoundEnd", "KillOneGetGun", CheckLastProp())[/lua] Please help if you know what the problem is. oh and I think CheckPlayerDeathRoundEnd is the only one prop hunt uses for deaths. Anyway, please help me figure this out.[/QUOTE] #prop_alive == "1" is your problem. It shouldn't be a string.
[QUOTE=Acecool;46064741]PlayerDeath should still be called... But, because I don't know much of anything at all about prop-hunt, consider this pseudo-code, albeit should work. If you know how many "props" you have at round start, save the number somewhere. On game-event player_disconnect, if player was prop, subtract 1 from our number. On hook PlayerDeath, if victim was prop, subtract 1 from our number. Then, your CheckPlayerDeathRoundEnd would only need to see if the number is !( x > 0 ) or you can use ( x < 1 ) for round end; == 1 for your shotgun... If you want to easily be able to target the last prop without going through a list, do this.... On round start, grab all valid props. Add them to a table. _table[ SteamID ] = PlayerObject; On hook PlayerDeath, or game-event player_disconnect, if _table[ SteamID ] then _table[ SteamID ] = nil; I'd also recommend having a counter variable too; saves time on table.Count... So subtract 1 in that case too.. If counter == 1 then _table[ table.GetFirstKey( _table ) ] is our player object... Give that prop the weapon.[/QUOTE] I think you made it way more complicated than it needs to be lol
In terms of "cost" and taking into consideration the small overhead ( references to player, plus storing steamid in a table; Lua is a language of tables so they're very optimized... ), it'd be cheaper. I scanned the thread, didn't see any solution to the initial question ( of how to give last player weapon ) which is why I answered with making the counter / table system. Looking at Disseminates' post, he's right but it is still more expensive.
I don't know what you're even talking about. What I'm saying is if the code that I already have can be fixed to work right then why would I go about doing that other jibbity jabbity stuff? I barely know what the hell I'm doing with basic coding, let alone all that other crap.
[QUOTE=Acecool;46064741]PlayerDeath should still be called... But, because I don't know much of anything at all about prop-hunt, consider this pseudo-code, albeit should work. If you know how many "props" you have at round start, save the number somewhere. On game-event player_disconnect, if player was prop, subtract 1 from our number. On hook PlayerDeath, if victim was prop, subtract 1 from our number.[/QUOTE] If we took my pseudocode for example, then we don't need to check for disconnects, since it will empty the table everytime someone dies, then fill it up with all Players, if they are in the Team Props. And only if the tablecount is 1, then it should give him the weapon. Maybe i'm wrong with #prop_alive == 1 and we should use if [URL="http://wiki.garrysmod.com/page/table/Count"]table.count[/URL](prop_alive) == 1 then
I just get an error with that [ERROR] gamemodes/prop_hunt/gamemode/player_class/class_prop.lua:21: attempt to call field 'count' (a nil value) 1. CheckLastProp - gamemodes/prop_hunt/gamemode/player_class/class_prop.lua:21 2. unknown - gamemodes/prop_hunt/gamemode/player_class/class_prop.lua:23 3. include - [C]:-1 4. IncludePlayerClasses - gamemodes/prop_hunt/gamemode/fretta/gamemode/shared.lua:291 5. unknown - gamemodes/prop_hunt/gamemode/sh_init.lua:14 6. include - [C]:-1 7. unknown - gamemodes/prop_hunt/gamemode/cl_init.lua:2
Count not count. i did an typo, but i linked the wiki. Read a bit into the things before you try just to c&p. That way you won't learn anything.
Alright. Still not doing anything though.
# can be used on tables to reliably count the number of elements if all elements are numerically keyed. If there are string keys mixed in, I can't recall if it will count or not count. table.Count will count regardless of key data-type. Feel free to add me on Steam; I tutor a lot of people, if you're willing to learn I'd be willing to give you a hand and help you pick up the jibby-jabby stuff and explain why it is less costly to execute ( although if it is only doing it on death, it isn't a huge deal unless you're looking to really squeeze every ounce of performance and remove as much lag / issues as possible ).
Alright so here's what I got going on, I changed the Give("weapon_shotgun") to "item_ar2_grenade" because I knew that item should work. So when I saved it like that (while I was last prop alive), it immediately spawned me the grenade but it also gave an error. So I added some bots to the server and tested it, when I slayed the other prop, which made me last prop alive, it didnt do anything. It only gives me it when I save it and I'm last prop alive. Don't know if any of this will help but figured it cant hurt. Here is the error. [ERROR] gamemodes/prop_hunt/gamemode/player_class/class_prop.lua:21: attempt to call method 'Give' (a nil value) 1. CheckLastProp - gamemodes/prop_hunt/gamemode/player_class/class_prop.lua:21 2. unknown - gamemodes/prop_hunt/gamemode/player_class/class_prop.lua:23 3. include - [C]:-1 4. IncludePlayerClasses - gamemodes/prop_hunt/gamemode/fretta/gamemode/shared.lua:291 5. unknown - gamemodes/prop_hunt/gamemode/sh_init.lua:14 6. include - [C]:-1 7. unknown - gamemodes/prop_hunt/gamemode/cl_init.lua:2
Give is a SERVERside command; it appears that the call is originating from the client file... Shared code can run on both client and server, but, just because it can doesn't mean it will... If it originates from the wrong realm, it could give that error.
It was probably fixed, but you have another problem that nobody seems to have grabbed: [code]hook.Add("CheckPlayerDeathRoundEnd", "KillOneGetGun", CheckLastProp())[/code] should just be [code]hook.Add("CheckPlayerDeathRoundEnd", "KillOneGetGun", CheckLastProp)[/code]
That just seems to disable it completely
It's sad how people ignore acecool even though what he says is usually exactly right and very efficient. Not saying that everyone in this thread did, it's just that I feel sorry for Acecool because he is very helpful and detailed yet some people without reason just ignore him cause they wanna be like the cool kids and hate on Ace.
[code]if !SERVER then print("You're doing it wrong .. learn the 3 golden words: SERVER, CLIENT and SHARED.") print("This has to be SERVERSIDE only!") return end function CheckLastProp() local prop_alive = {} for k,ply in pairs( team.GetPlayers(TEAM_PROPS) ) do if ply:Alive() then table.insert( prop_alive, ply ) end end if #prop_alive == 1 then prop_alive[1]:Give("weapon_shotgun") end end hook.Add("CheckPlayerDeathRoundEnd", "KillOneGetGun", CheckLastProp)[/code]
ok so here is the code i have [lua] function CheckLastProp() local prop_alive = {} for k,ply in pairs( team.GetPlayers(TEAM_PROPS) ) do if ply:Alive() then table.insert( prop_alive, ply ) end end if table.Count(prop_alive) == 1 then prop_alive[1]:Give("item_ar2_grenade") end end hook.Add("PlayerDeath", "KillOneGetGun", CheckLastProp)[/lua] With the hook as PlayerDeath it works but only when a hunter dies because PlayerDeath isnt called when a prop is killed. I've tried using other hooks but havent found one that works. Suggestion? edit: Also, I changed the "item_ar2_grenade" to "weapon_smg1". And like I said, when a hunter dies the last prop gets the gun, but the view is messed up and the prop player cant damage the hunter player, also when I changed to third person mode as the prop with gun, it showed my player model and everything. It was as if I was a hunter. Any idea on how to fix this?
Sorry, you need to Log In to post a reply to this thread.