A pvp system

I’m making a gamemode similar to sandbox and I was wondering If any one could or knows how to make a pvp like system.

The system would be something like this:

When a player joins the game they cannot be killed, they have some way of not dying from a protection system like god mode which initiates when they get inside the game.

To be able to be killed you will need to type a message in chat. Example: “!PvpOn” When you say this it turns your god mode off and makes it so you can be killable.

When you have god mode on or “!pvpoff” you do not have weapons when you spawn but when you activate the pvp you will spawn with weapons the next time you die. To make it so that when you use !pvpoff it will strip you of your weapons or it could slay/kill you.



function PvPToggle( ply, text, commands, args )
    if (string.sub(text, 1, 6) == "!pvpon") then
		umsg.Start("YesPvP", ply)
		umsg.End()
	end
	
	if (string.sub(text, 1, 7) == "!pvpoff") then
		umsg.Start("NoPvP", ply)
	end
end
hook.Add( "PlayerSay", "ChatCommands", PvPToggle )

function PvPoff(ply)
	for k, v in pairs (player.GetAll()) do
		v:ChatPrint("".. ply:Nick() .." is not PvP flagged!")
	end
	ply:PrintMessage(HUD_PRINTCENTER, "You can no longer recieve damage")
       ply:GodEnable()
	   ply:StripWeapons()
	   ply:Give("phys_gun")
	   ply:Give("phys_cannon")
	   ply:Give("gmod_tool")
end
usermessage.Hook("NoPvP", PvPoff)

function PvPon(ply)
	for k, v in pairs (player.GetAll()) do
		v:ChatPrint("".. ply:Nick() .." is PvP flagged!")
	end
	ply:PrintMessage(HUD_PRINTCENTER, "You can now recieve damage")
		ply:GodDisable()
		ply:Give("weapon_357")
		ply:Give("weapon_alyxgun")
		ply:Give("weapon_annabelle")
		ply:Give("weapon_ar2")
		ply:Give("weapon_bugbait")
		ply:Give("weapon_crossbow")
		ply:Give("weapon_crowbar")
		ply:Give("weapon_frag")
		ply:Give("weapon_physcannon")
		ply:Give("weapon_pistol")
		ply:Give("weapon_rpg")
		ply:Give("weapon_shotgun")
		ply:Give("weapon_smg1")
		ply:Give("weapon_stunstick")
end
usermessage.Hook("YesPvP", PvPon)

Modify it to your liking.

[editline]02:39AM[/editline]

lua/autorun/server/pvp.lua by the way.

Thank you kind sir! I wont take credit for your work but only my modifications.

Meh, I need stuff to do. Although, it is 3am. I’ll get on my laptop and lie in the bed, wait for you response wether or not it works.

Ill test it right now then ill tell you if it works.

[editline]01:09AM[/editline]

I put it in Gamemodename/Gamemode right? I tried typing in !pvpon and it didnt work and I tried running it from the console and it said this in console as a error:
:1: ‘=’ expected near ‘<eof>’

Yes that’s a messed up script. I’m too tired now but I might make it for you tomorrow :slight_smile:

Okay, thanks guys.

Only thing I can see is that I left out a umsg.End().

Try to replace the first function with this’



function PvPToggle( ply, text, commands, args )
    if (string.sub(text, 1, 6) == "!pvpon") then
		umsg.Start("YesPvP", ply)
		umsg.End()
	end
	
	if (string.sub(text, 1, 7) == "!pvpoff") then
		umsg.Start("NoPvP", ply)
		umsg.End()
	end
end
hook.Add( "PlayerSay", "ChatCommands", PvPToggle )


and put the whole script in lua/autorun/server

Usermessages are a clientside library whereas all of that script should be serverside. Also you should give the weapons on the PlayerLoadout hook so that they are given out each time they spawn and not just when they toggle PVP.

[editline]02:55PM[/editline]

I edited BusyMonkey’s code a bit to get it working.
[lua]
function PvPToggle( ply, text, commands, args )
if (string.sub(text, 1, 6) == “!pvpon”) then
PVPOn(ply)
end

if (string.sub(text, 1, 7) == "!pvpoff") then
	PVPOff(ply)
end

end
hook.Add( “PlayerSay”, “ChatCommands”, PvPToggle )

function PVPOff(ply)
for k, v in pairs (player.GetAll()) do
v:ChatPrint(""… ply:Nick() …" is not PvP flagged!")
end
ply:PrintMessage(HUD_PRINTCENTER, “You can no longer recieve damage”)
ply:GodEnable()
ply:StripWeapons()
ply:Give(“weapon_physgun”)
ply:Give(“weapon_physcannon”)
ply:Give(“gmod_tool”)
ply.pvp = false
end

function PVPOn(ply)
for k, v in pairs (player.GetAll()) do
v:ChatPrint(""… ply:Nick() …" is PvP flagged!")
end
ply:PrintMessage(HUD_PRINTCENTER, “You can now recieve damage”)
ply:GodDisable()
ply:Give(“weapon_357”)
ply:Give(“weapon_alyxgun”)
ply:Give(“weapon_annabelle”)
ply:Give(“weapon_ar2”)
ply:Give(“weapon_bugbait”)
ply:Give(“weapon_crossbow”)
ply:Give(“weapon_crowbar”)
ply:Give(“weapon_frag”)
ply:Give(“weapon_physcannon”)
ply:Give(“weapon_pistol”)
ply:Give(“weapon_rpg”)
ply:Give(“weapon_shotgun”)
ply:Give(“weapon_smg1”)
ply:Give(“weapon_stunstick”)
ply.pvp = true
end

hook.Add(“PlayerLoadout”, “PvPWeapons”, function(ply)
if ply.pvp then
ply:Give(“weapon_357”)
ply:Give(“weapon_alyxgun”)
ply:Give(“weapon_annabelle”)
ply:Give(“weapon_ar2”)
ply:Give(“weapon_bugbait”)
ply:Give(“weapon_crossbow”)
ply:Give(“weapon_crowbar”)
ply:Give(“weapon_frag”)
ply:Give(“weapon_physcannon”)
ply:Give(“weapon_pistol”)
ply:Give(“weapon_rpg”)
ply:Give(“weapon_shotgun”)
ply:Give(“weapon_smg1”)
ply:Give(“weapon_stunstick”)
else
ply:Give(“weapon_physgun”)
ply:Give(“weapon_physcannon”)
ply:Give(“gmod_tool”)
return true
end
end)
[/lua]

Right :doh:

I put it into gamemodename/gamemode and went into game and typed !pvpon and it didnt work. Then I tried typing lua_run pvp.lua and it said this:
> pvp.lua…
:1: ‘=’ expected near ‘<eof>’

I think I’m putting it in wrong or something.

edit:

Still doesn’t work. Explain where I have to put it better please.

[editline]01:07AM[/editline]

or in init.lua

[editline]01:08AM[/editline]

of your gamemode.

I tried gamemodename/gamemode/lua/autorun/server and it didnt work. Ill try putting it into the init.lua and ill post back if it works.

I might of found out why it didnt work for me. My sandboxes init.lua and my gamemode init.lua are not there. I think I might of deleted my sandboxes init from a previous work in progress and i never added it into my gamemode.

Can somebody copy over the sandbox’s init.lua for me so I can fix this problem?

Sandbox gamemode is in the GFC; meaning that if you dont have them in your folder, they will be created, when running Gmod.
This also mean that you cannot add/edit stuff in the sandbox files.

gamemodename/gamemode/lua/autorun/server

There’s no such thing as that path. Dont put it in your gamemode folder, just lua/autorun/server

[editline]06:27AM[/editline]

or in gamemodes/gamemodename/gamemode/init.lua

[editline]06:28AM[/editline]

Just not if the gamemode is sandbox.

It works nicely guys, thanks for the help.