• table.Random(player.GetAll()) is nil? [Lua]
    5 replies, posted
Before you say anything, yeah I'm not that good at programming Lua, but everyone has to start somewhere, right? Anyway, to the point: I'm trying to make a simple gamemode based off of the kids' game "Cops and Robbers" (or at least how I played it back in the days), where a few players are assigned as Cops, and the rest become Runners. The Runners need to run away from the cops and avoid getting tagged (Getting damaged by the cops' stunsticks). If they get tagged, they teleport to the jail, unable to escape. However the free Runners are able to hit the jailed Runners with their crowbar to release them. Cops need to catch everyone at once, the runners need to stay alive for a time limit. Then everything is reset and the game randomly chooses cops again. This is where I have the problem: [CODE]-- Function for Round Restart function roundRestart() game.CleanUpMap(); local Players = player.GetAll() for i = 1, table.Count(Players) do local ply = Players[i] ply:SetTeam(6) -- Sets every player to Team 6, the unassigned end -- Randomly Assign a Warden local randomwarden = table.Random(player.GetAll()) randomwarden:SetWarden() -- Randomly chooses the cops local copcount = 0 while copcount + 1 <= #player:GetAll() * copPercentage do local randomcop = table.Random(player.GetAll()) if randomcop:Team() == 6 then randomcop:SetCop() copcount = copcount + 1 end end -- Randomly assigns the Armed Runners local armedcount = 0 while armedcount < armedRunners do local randomarmed = table.Random(player.GetAll()) if randomarmed:Team() == 6 then randomarmed:SetArmedRunner() armedcount = armedcount + 1 end end -- The rest becomes Runners while team.NumPlayers(6) != 0 do local remaining = table.Random(player.GetAll()) if remaining:Team() == 6 then remaining:SetRunner() end end -- Spawns all players ply:Spawn(); end[/CODE] It is probably possible to have a table of all players in team 6 and remove the spawned players, so that the game doesn't loop through [I]all[/I] players, but only the unassigned. Just don't know how yet :P Anyway According to the console, 'randomwarden' is a nil value [CODE][ERROR] gamemodes/copsandrobbers/gamemode/init.lua:46: attempt to index local value 'randomwarden' (a nil value)[/CODE] It's probably obvious, but I can't figure it out, so some help would be greatly appreciated. Another thing, the Warden is the one cop that gets to place the 'Jail'. I want the jail to be a circle of invisible walls in a specified size, that only Team 3 collides with (Jailed Runners). That way everyone can run through jail, but the jailed players can't run out! (They are teleported to jail when catched). When they are released, they can simply run through the invisible walls. [B]If anyone knows how to script such an entity, that would be appreciated too[/B] :P Bonus points if the size is modifiable (with console commands), and if it can draw a colored circle on the ground to indicate the position! ;)
Have you created the meta method for :SetWarden()? If you want to easy fix it, just... [lua] local randomwarden = player.GetAll()[math.random(1,#player.GetAll())] [/lua] I know, i know, table random returns a random element of the table, but i don't know how to make this works because sometimes i get the same issue
Tried using yours, still errors out. Also tried replacing 'randomwarden' with 'ply' just for the heck of it, because the function goes like this: [CODE]local ply = FindMetaTable("Player") [More code between] -- Function to set player as Warden function ply:SetWarden() self:SetTeam( 5 ) self:SetModel("models/player/combine.mdl") self:SetPlayerColor( teamColors[1] ) self:SetHealth( 100 ) self:StripWeapons() self:Give("weapon_stunstick") self:Give("cr_jail") self:Freeze( false ) self:SetWalkSpeed( 325 ) self:SetRunSpeed( 575 ) self:SetNoCollideWithTeammates( false ) -- Alert that the player is Warden print( self:GetName() .. " has become a Warden!") end[/CODE] That gave the same error. That function is located in 'player.lua' which is loaded by the server only, by the way. But the function should work, as I become the Warden when I spawn in, and when I test it with friends, they do become jailed and freed depending on what hit them.
Are you developing this by yourself btw? Sounds like a fun gamemode and I'd like to help if possible. Not claiming to be superduper good at lua, but the offer is there :)
[QUOTE=Blasteh;45120772]Are you developing this by yourself btw? Sounds like a fun gamemode and I'd like to help if possible. Not claiming to be superduper good at lua, but the offer is there :)[/QUOTE] I am making this all by myself, so thanks, but no thanks. I prefer to keep projects like this to myself or with friends, so that the project feels more like a personal accomplishment. However if I get completely stuck, I might change my mind xD But thanks ;D
Solved myself by completely removing the table.Random part. Replaced it with this, in case anyone has the same problems: [CODE]-- Randomly sets a Warden and removes him from the table local randomply = math.random(1,table.Count(allplayers)) for k,v in pairs(allplayers) do if k == randomply then v:SetWarden() table.remove(allplayers, k) end end [/CODE] It simply picks a random player number, then loops through all players until it reaches the player with that designated number, then it sets him as Warden and removes him from the table. I have done this on all roles that are to be randomly assigned.
Sorry, you need to Log In to post a reply to this thread.