How to constantly run the same commands on everyone on the server?

I want to run this :

v:UnSpectate()
v:StripWeapons()
v:StripAmmo()
v:Spawn()

On everyone who is in spectator mode, and I want it doing it every second or so. I could use GetMoveType == 2 I guess, but Im not very good at coding and I wouldnt know how to do that. Can someone tell me?

Thanks in advance for help :smiley:

You’d like to Spawn the guy every second?
Why the **** would you want to do that?

It depends on the gamemode you’re running and the hooks it uses. Which gamemode is this for?

Im using a team deathmatch server, I want to spawn everyone who’s in spectator mode into the game and assign a team. I dont want to spawn the guy every second, I want to check for people that are in spectator mode every second and spawn those who are

Then why not remove the spectators system?



function CheckForOBS()
     for k, v in pairs( player.GetAll() )do
          if(v:GetMoveType() == MOVETYPE_OBSERVER)then
               v:UnSpectate() 
               v:StripWeapons()
               v:StripAmmo()
               v:SetTeam( Whatever Team You Want Here, or your own function to get a random team)
               v:Spawn()
          end
     end
end
timer.Create("CheckforOBS", 1, 0, CheckForOBS)


I dont know how to remove it, I’ll upload the code so you can show me how to delete it if you want… I’ll test that now mindfull, thanks

[editline]10th August 2015[/editline]

When I run that (I put it in init.lua at the bottom) I get :

EDIT:

[ERROR] gamemodes/applesmatch/gamemode/init.lua:629: bad argument #4 to ‘Create’
(Function expected, got nil)
1. Create -[C]:-1

Do I need to put the timer.Create in a different area?

You’ll want to put that timer under the function’s end.

Thanks, I’ll test that in a sec

I’d recommend to do this like this:



timer.Create( "CheckforOBS", 1, 0, function()
     for k, v in pairs( player.GetAll() )do
          if(v:GetMoveType() == MOVETYPE_OBSERVER)then
               v:UnSpectate() 
               v:StripWeapons()
               v:StripAmmo()
               v:SetTeam( Whatever Team You Want Here, or your own function to get a random team)
               v:Spawn()
          end
     end
end )

This way there’s no confusion of whether to add the () or where to put the function.

You could also use team.GetPlayers() instead of player.GetAll and a move type check.

Even better, use the OnPlayerChangeTeam hook to check when people join the spectators, and instantly run those functions on them to spawn them back in. Why resort to a constant check of a condition when you can just only do it when the actual condition happens?

Doesnt work, when we do end up spawning were not in a team and our weapons are stripped (I actually wanted them unstripped, dont know the code for that and messed up typing) And it switches all people currentley in the game to have their weapons and ammo stripped aswell

EDIT:

Posted this when I hadnt seen last 2 posts, i’ll try them now

EDITx2: There was some other conflicting code, cant get a friend to come test it. Tried spawning bots, that didnt work. Gonna try with a player in a sec, I’ll get back to you guys

[editline]10th August 2015[/editline]

[editline]10th August 2015[/editline]

Right, turns out that the bots are spawning in and so are people they are just invisible. I !goto the bots and they are sometimes in walls, just moving around. This is the code:

      if(v:GetMoveType() == 10)then
           v:UnSpectate( 1 )
		   v:GetViewModel( 0 )
		   v:SetMoveType( 2 )
		   v:UnSpectate() 
           v:SetTeam( PlaceMeOnTheLowestTeamAvaialable )
           v:Spawn()

Wondering if there is some other variable in the whole code, Im going through it now

If you tried that hook and it didn’t work, you’re likely doing something wrong. If everyone lost their weapons, you likely applied those lines to every player when someone joined spectators. You will obviously have to only do it on the actual player switching teams. And strip his weapons before you spawn him, otherwise he will spawn, get his new teams weapons, and then have them stripped again. Alternatively, give him the weapons he should spawn with yourself instead of stripping and letting the team code do give him weapons.

Managed to fix it anyway, was missing a simple line of code specific to the gamemode. Thanks yall for your help :smiley: