I need help in making an area where you can pvp

  1. I want to achieve a working pvp zone where players can fight with weapons but can still hold weapons while not in it.

  2. The issue is I’m bad at coding and cant find a vid on it

  3. I have thought of making the whole rest of map a giant safezone

1 Like
local zone = script.Parent -- the area

zone.Touched:Connect(function(part)
	if part.Name == "Torso" or part.Name == "UpperTorso" then
		local player = game.Players:GetPlayerFromCharacter(part.Parent)
		if player.Team == game.Teams.NonPVP then
			player.Team = game.Teams.PVP
		end
	end
end)

zone.TouchEnded:Connect(function(part)
	if part.Name == "Torso" or part.Name == "UpperTorso" then
		local player = game.Players:GetPlayerFromCharacter(part.Parent)
		if player.Team == game.Teams.PVP then
			player.Team = game.Teams.NonPVP
		end
	end
end)

Place this script in the area object. Also make two teams called “PVP” and “NonPVP”.
robloxapp-20240413-1457096.wmv (1.8 MB)

Dont forget to check if both players in the PVP team when using a weapon

thank you, Ima try this later :slight_smile:

2 Likes


for me its not working and i have no clue why

изображение

изображение

SpawnLocation
изображение


idk why it doesnt work

Try these
Sphere:
–This will find character with a root from the center.
TimeDelay = 1

local function FindTargets()
local Center = script.Parent.HumanoidRootPart.Position

local nearestTarget = nil

dist = 100 --The max distance/radius

for i, Target in ipairs(workspace:GetChildren()) do
	local Root = Target:FindFirstChild("HumanoidRootPart") --Finds a root
	if Root then --Checks if a root is found
		local disttotarget = (Root.Position - Center).Magnitude --the distance bewteen the center and the target
		if disttotarget < dist and disttotarget > 0 then
			--code for each root in radius
		else
			--opposite
		end
	end
end

end

repeat
wait(TimeDelay)
FindTargets() --Starts to find the nearest target.
until script.Disabled == true

Cube:
TimeDelay = 1
ShowZone = false --Show Points of the zone to have visual

local function FindTargets()
local Center = script.Parent.Position

local nearestTarget = nil

Zone = Vector3.new(5,5,5) --script.Parent.Size

--script.Parent.Orientation = Vector3.new(0,0,0) --Must be set this way if you use size as the zone.

if ShowZone == true then
	ShowZone = false
	Point = Instance.new("Part",workspace)
	Point.Name = "X1"
	Point.Anchored = true
	Point.CanCollide = false
	Point.Size = Vector3.new(1,1,1)
	Point.BrickColor = BrickColor.Yellow()
	Point.Position = Vector3.new((Zone.X/2 + Center.X),Center.Y,Center.Z)

	Point = Instance.new("Part",workspace)
	Point.Name = "X2"
	Point.Anchored = true
	Point.CanCollide = false
	Point.Size = Vector3.new(1,1,1)
	Point.BrickColor = BrickColor.Yellow()
	Point.Position = Vector3.new((Center.X - Zone.X/2),Center.Y,Center.Z)

	Point = Instance.new("Part",workspace)
	Point.Name = "Y1"
	Point.Anchored = true
	Point.CanCollide = false
	Point.Size = Vector3.new(1,1,1)
	Point.BrickColor = BrickColor.Yellow()
	Point.Position = Vector3.new(Center.X,(Zone.Y/2 + Center.Y),Center.Z)

	Point = Instance.new("Part",workspace)
	Point.Name = "Y2"
	Point.Anchored = true
	Point.CanCollide = false
	Point.Size = Vector3.new(1,1,1)
	Point.BrickColor = BrickColor.Yellow()
	Point.Position = Vector3.new(Center.X,(Center.Y - Zone.Y/2),Center.Z)

	Point = Instance.new("Part",workspace)
	Point.Name = "Z1"
	Point.Anchored = true
	Point.CanCollide = false
	Point.Size = Vector3.new(1,1,1)
	Point.BrickColor = BrickColor.Yellow()
	Point.Position = Vector3.new(Center.X,Center.Y,(Zone.Z/2 + Center.Z))

	Point = Instance.new("Part",workspace)
	Point.Name = "Z2"
	Point.Anchored = true
	Point.CanCollide = false
	Point.Size = Vector3.new(1,1,1)
	Point.BrickColor = BrickColor.Yellow()
	Point.Position = Vector3.new(Center.X,Center.Y,(Center.Z - Zone.Z/2))
end


for i, Target in ipairs(workspace:GetChildren()) do
	if Target.Name ~= script.Parent.Name then --Makes sure to not pick its self. --Remove .name or this "if" if it messes with anything, the .name is not enough, or it is not needed.
		local Root = Target:FindFirstChild("HumanoidRootPart") --Finds a root
		if Root then --Checks if a root is found
			if Root.Position.X < (Zone.X/2 + Center.X) and Root.Position.X > (Center.X - Zone.X/2) and Root.Position.Y < (Zone.Y/2 + Center.Y) and Root.Position.Y > (Center.Y - Zone.Y/2) and Root.Position.Z < (Zone.Z/2 + Center.Z) and Root.Position.Z > (Center.Z - Zone.Z/2) then
				--code for each root in zone
			else
				--code for every other root not in zone
			end
		end
	end
end

end

repeat
wait(TimeDelay)
FindTargets() --Starts to find the nearest target.
until script.Disabled == true

local zone = script.Parent -- the area

zone.Touched:Connect(function(part)
	if part.Name == "Torso" or part.Name == "UpperTorso" then
		local player = game.Players:GetPlayerFromCharacter(part.Parent)
		if player.Team == game.Teams.NonPVP then
			player.Team = game.Teams.PVP
		end
	end
end)

zone.TouchEnded:Connect(function(part)
	if part.Name == "Torso" or part.Name == "UpperTorso" then
		local player = game.Players:GetPlayerFromCharacter(part.Parent)
		if player.Team == game.Teams.PVP then
			player.Team = game.Teams.NonPVP
		end
	end
end)

I forgot to say that this is a server script…
Add some prints to check the code

2 Likes

oh thats why it didnt work tysm

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.