How to detect a players death?

Hi, is there a way i can store a variable for a specific player, and then make an if statement saying wether they died. Something like this, but my script isnt working:

The part that I’m trying to add it to…

local function startEvent()
	
	print("started")
	
	local plr1CurrentKills = 0
	local plr2CurrentKills = 0
	
	local SwordGiver = game.ServerStorage.Sword
	

	local Sword1 = SwordGiver:Clone()
	local char1 = standingOnArena1Player1.Character
	
	print("First Variables")
	
	if char1 then
		local humanoid1 = char1:FindFirstChild("Humanoid")
		
		print("After Humanoid 1")
		
		humanoid1.Died:Connect(function()
			
			print("Detected Death")
			
			plr2CurrentKills = plr2CurrentKills + 1
			
			print("Added Kills")
			
			if plr1CurrentKills < 5 then
				
				print("Less than 5")
				
				if humanoid1 then
					
					print("Detected humanoid")
					
					humanoid1.RespawnLocation = game.Workspace.Arena1Team1
					print("Set respawn")
					Sword1.Parent = standingOnArena1Player1.Backpack
					print("Gave sword")
					humanoid1:EquipTool(Sword1)
					print("Complete")
				end
			end
		end)		
	end

	local Sword2 = SwordGiver:Clone()
	local char2 = standingOnArena1Player2.Character
	if char2 then
		local humanoid2 = char2:FindFirstChild("Humanoid")
		
		humanoid2.Died:Connect(function()
			plr1CurrentKills = plr1CurrentKills + 1
			if plr2CurrentKills < 5 then
				if humanoid2 then
					humanoid2.RespawnLocation = game.Workspace.Arena1Team2
					Sword2.Parent = standingOnArena1Player2.Backpack
					humanoid2:EquipTool(Sword2)
				end
			end
		end)
	end
end

The whole script:

local Players = game:GetService("Players")
local player = game.Players.LocalPlayer

local part1 = workspace.Arena1Player1
local part2 = workspace.Arena1Player2

local team1 = workspace.Arena1Team1
local team2 = workspace.Arena1Team2

local standingOnArena1Player1 = nil
local standingOnArena1Player2 = nil


local RemoteEvent = game.ReplicatedStorage:FindFirstChild("RemoteEvent")

local function startEvent()
	
	print("started")
	
	local plr1CurrentKills = 0
	local plr2CurrentKills = 0
	
	local SwordGiver = game.ServerStorage.Sword
	

	local Sword1 = SwordGiver:Clone()
	local char1 = standingOnArena1Player1.Character
	
	print("First Variables")
	
	if char1 then
		local humanoid1 = char1:FindFirstChild("Humanoid")
		
		print("After Humanoid 1")
		
		humanoid1.Died:Connect(function()
			
			print("Detected Death")
			
			plr2CurrentKills = plr2CurrentKills + 1
			
			print("Added Kills")
			
			if plr1CurrentKills < 5 then
				
				print("Less than 5")
				
				if humanoid1 then
					
					print("Detected humanoid")
					
					humanoid1.RespawnLocation = game.Workspace.Arena1Team1
					print("Set respawn")
					Sword1.Parent = standingOnArena1Player1.Backpack
					print("Gave sword")
					humanoid1:EquipTool(Sword1)
					print("Complete")
				end
			end
		end)		
	end

	local Sword2 = SwordGiver:Clone()
	local char2 = standingOnArena1Player2.Character
	if char2 then
		local humanoid2 = char2:FindFirstChild("Humanoid")
		
		humanoid2.Died:Connect(function()
			plr1CurrentKills = plr1CurrentKills + 1
			if plr2CurrentKills < 5 then
				if humanoid2 then
					humanoid2.RespawnLocation = game.Workspace.Arena1Team2
					Sword2.Parent = standingOnArena1Player2.Backpack
					humanoid2:EquipTool(Sword2)
				end
			end
		end)
	end
end

RemoteEvent.OnServerEvent:Connect(function() -- if any player presses the play button, executes event
	if standingOnArena1Player2 ~= nil and standingOnArena1Player1 ~= nil then
		--Hide playbutton gui
		standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = false
		standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = false
		--Teleports
		workspace[standingOnArena1Player1.Name].HumanoidRootPart.CFrame = CFrame.new(team1.Position.X,team1.Position.Y,team1.Position.Z)
		workspace[standingOnArena1Player1.Name].HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(90), 0)
		workspace[standingOnArena1Player2.Name].HumanoidRootPart.CFrame = CFrame.new(team2.Position.X,team2.Position.Y,team2.Position.Z)
		workspace[standingOnArena1Player2.Name].HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(270), 0)
		local gameRunning = true
		startEvent() -- triggers function
	end
end)


-----------------------------------------------------

--				PART1 Events				 --

-----------------------------------------------------

-- Touched functions [Events now set each part that was touched to the players name instead of the opposite button]

part1.Touched:Connect(function(TouchedPart) -- Touched

	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)

	if Player then -- Check if it found character
		if standingOnArena1Player1 == nil then -- Check if not standing and if not occupied

			part1.BrickColor = BrickColor.new("Teal") -- Set color
			standingOnArena1Player1 = Player
		end	
	end
end)

part1.TouchEnded:Connect(function(TouchedPart)

	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)

	if Player then -- Check if it found character
		if standingOnArena1Player1 ~= nil then -- Check if standing and is occupied
			if  standingOnArena1Player1.PlayerGui.ScreenGui.Enabled == true then
				standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = false
			end
			part1.BrickColor = BrickColor.new("Smoky grey") -- Set color
			standingOnArena1Player1 = nil

			if standingOnArena1Player2 ~= nil then
				if standingOnArena1Player2.PlayerGui.ScreenGui.Enabled == true then
					standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = false
				end
			end
		end	
	end
end)

-----------------------------------------------------

--				PART2 Events				 --

-----------------------------------------------------

part2.Touched:Connect(function(TouchedPart) -- Touched

	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)

	if Player then -- Check if it found character
		if standingOnArena1Player2 == nil then -- Check if not standing and if not occupied

			part2.BrickColor = BrickColor.new("Teal") -- Set color
			standingOnArena1Player2 = Player
		end	
	end
end)

part2.TouchEnded:Connect(function(TouchedPart)

	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)

	if Player then -- Check if it found character
		if standingOnArena1Player2 ~= nil then -- Check if standing and is occupied
			if   standingOnArena1Player2.PlayerGui.ScreenGui.Enabled == true then
				standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = false
			end
			part2.BrickColor = BrickColor.new("Smoky grey") -- Set color
			standingOnArena1Player2 = nil

			if standingOnArena1Player1 ~= nil then
				if  standingOnArena1Player1.PlayerGui.ScreenGui.Enabled == true then
					standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = false
				end
			end
		end	
	end
end)

while task.wait(0) do
	if standingOnArena1Player2 ~= nil and standingOnArena1Player1 ~= nil then -- checks if both players are on top
		standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = true
		standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = true
	end
end

Any help would be greatly appreticiated!

it would probably be easier to put the death detection in the damage script. Put the death detection before the damage and disconnect it after damage is done. You can also use HumanoidState to check if they are dead after damage is done

2 Likes

How would i transfer the data of wether the player is dead back over to this server script?

Also, I have multiple ‘arenas’ so I need to make sure that its only looking for ‘player1’ and ‘player2’ death not the whole server and idk how to do that

you could use a bindable event it’s like a remote event but it doesn’t require a local script to send to a server script (if you’re trying to send from local to server then use remote event)

1 Like

how does the damage work? is it in a script in a sword, or is it like a server script service script handling all the damage?

1 Like

Thanks for your help I really appreciate it btw, the damage script is in a sword which is stored in server storage. Here is the script:

--Rescripted by Luckymaxer
--EUROCOW WAS HERE BECAUSE I MADE THE PARTICLES AND THEREFORE THIS ENTIRE SWORD PRETTY AND LOOK PRETTY WORDS AND I'D LIKE TO DEDICATE THIS TO MY FRIENDS AND HI LUCKYMAXER PLS FIX SFOTH SWORDS TY LOVE Y'ALl
--Updated for R15 avatars by StarWars
--Re-updated by TakeoHonorable

Tool = script.Parent
Handle = Tool:WaitForChild("Handle")

local damageModule = require(game.ServerStorage.DamageModule)

function Create(ty)
	return function(data)
		local obj = Instance.new(ty)
		for k, v in pairs(data) do
			if type(k) == 'number' then
				v.Parent = obj
			else
				obj[k] = v
			end
		end
		return obj
	end
end

local BaseUrl = "rbxassetid://"

Players = game:GetService("Players")
Debris = game:GetService("Debris")
RunService = game:GetService("RunService")

DamageValues = {
	BaseDamage = 5,
	SlashDamage = 10,
	LungeDamage = 30
}

--For R15 avatars
Animations = {
	R15Slash = 522635514,
	R15Lunge = 522638767
}

Damage = DamageValues.BaseDamage

Grips = {
	Up = CFrame.new(0, 0, -1.70000005, 0, 0, 1, 1, 0, 0, 0, 1, 0),
	Out = CFrame.new(0, 0, -1.70000005, 0, 1, 0, 1, -0, 0, 0, 0, -1)
}

Sounds = {
	Slash = Handle:WaitForChild("SwordSlash"),
	Lunge = Handle:WaitForChild("SwordLunge"),
	Unsheath = Handle:WaitForChild("Unsheath")
}

ToolEquipped = false

--For Omega Rainbow Katana thumbnail to display a lot of particles.
for i, v in pairs(Handle:GetChildren()) do
	if v:IsA("ParticleEmitter") then
		v.Rate = 20
	end
end

Tool.Grip = Grips.Up
Tool.Enabled = true

function IsTeamMate(Player1, Player2)
	return (Player1 and Player2 and not Player1.Neutral and not Player2.Neutral and Player1.TeamColor == Player2.TeamColor)
end

function TagHumanoid(humanoid, player)
	local Creator_Tag = Instance.new("ObjectValue")
	Creator_Tag.Name = "creator"
	Creator_Tag.Value = player
	Debris:AddItem(Creator_Tag, 2)
	Creator_Tag.Parent = humanoid
end

function UntagHumanoid(humanoid)
	for i, v in pairs(humanoid:GetChildren()) do
		if v:IsA("ObjectValue") and v.Name == "creator" then
			v:Destroy()
		end
	end
end

function Blow(Hit)
	if not Hit or not Hit.Parent or not CheckIfAlive() or not ToolEquipped then
		return
	end
	local RightArm = Character:FindFirstChild("Right Arm") or Character:FindFirstChild("RightHand")
	if not RightArm then
		return
	end
	local RightGrip = RightArm:FindFirstChild("RightGrip")
	if not RightGrip or (RightGrip.Part0 ~= Handle and RightGrip.Part1 ~= Handle) then
		return
	end
	local character = Hit.Parent
	if character == Character then
		return
	end
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if not humanoid or humanoid.Health == 0 then
		return
	end
	local player = Players:GetPlayerFromCharacter(character)
	if player and (player == Player or IsTeamMate(Player, player)) then
		return
	end
	damageModule(humanoid,Player,Damage)
end


function Attack()
	Damage = DamageValues.SlashDamage
	Sounds.Slash:Play()

	if Humanoid then
		if Humanoid.RigType == Enum.HumanoidRigType.R6 then
			local Anim = Instance.new("StringValue")
			Anim.Name = "toolanim"
			Anim.Value = "Slash"
			Anim.Parent = Tool
		elseif Humanoid.RigType == Enum.HumanoidRigType.R15 then
			local Anim = Tool:FindFirstChild("R15Slash")
			if Anim then
				local Track = Humanoid:LoadAnimation(Anim)
				Track:Play(0)
			end
		end
	end	
end

function Lunge()
	Damage = DamageValues.LungeDamage

	Sounds.Lunge:Play()
	
	if Humanoid then
		if Humanoid.RigType == Enum.HumanoidRigType.R6 then
			local Anim = Instance.new("StringValue")
			Anim.Name = "toolanim"
			Anim.Value = "Lunge"
			Anim.Parent = Tool
		elseif Humanoid.RigType == Enum.HumanoidRigType.R15 then
			local Anim = Tool:FindFirstChild("R15Lunge")
			if Anim then
				local Track = Humanoid:LoadAnimation(Anim)
				Track:Play(0)
			end
		end
	end	
	--[[
	if CheckIfAlive() then
		local Force = Instance.new("BodyVelocity")
		Force.velocity = Vector3.new(0, 10, 0) 
		Force.maxForce = Vector3.new(0, 4000, 0)
		Debris:AddItem(Force, 0.4)
		Force.Parent = Torso
	end
	]]
	
	wait(0.2)
	Tool.Grip = Grips.Out
	wait(0.6)
	Tool.Grip = Grips.Up

	Damage = DamageValues.SlashDamage
end

Tool.Enabled = true
LastAttack = 0

function Activated()
	if not Tool.Enabled or not ToolEquipped or not CheckIfAlive() then
		return
	end
	Tool.Enabled = false
	local Tick = RunService.Stepped:wait()
	if (Tick - LastAttack < 0.2) then
		Lunge()
	else
		Attack()
	end
	LastAttack = Tick
	--wait(0.5)
	Damage = DamageValues.BaseDamage
	local SlashAnim = (Tool:FindFirstChild("R15Slash") or Create("Animation"){
		Name = "R15Slash",
		AnimationId = BaseUrl .. Animations.R15Slash,
		Parent = Tool
	})
	
	local LungeAnim = (Tool:FindFirstChild("R15Lunge") or Create("Animation"){
		Name = "R15Lunge",
		AnimationId = BaseUrl .. Animations.R15Lunge,
		Parent = Tool
	})
	Tool.Enabled = true
end

function CheckIfAlive()
	return (((Player and Player.Parent and Character and Character.Parent and Humanoid and Humanoid.Parent and Humanoid.Health > 0 and Torso and Torso.Parent) and true) or false)
end

function Equipped()
	Character = Tool.Parent
	Player = Players:GetPlayerFromCharacter(Character)
	Humanoid = Character:FindFirstChildOfClass("Humanoid")
	Torso = Character:FindFirstChild("Torso") or Character:FindFirstChild("HumanoidRootPart")
	if not CheckIfAlive() then
		return
	end
	ToolEquipped = true
	Sounds.Unsheath:Play()
end

function Unequipped()
	Tool.Grip = Grips.Up
	ToolEquipped = false
end

Tool.Activated:Connect(Activated)
Tool.Equipped:Connect(Equipped)
Tool.Unequipped:Connect(Unequipped)

Connection = Handle.Touched:Connect(Blow)

I have learnt about bindable events and have put one into my scripts. Its not working however, but I think it’s because the game is not detecting the players death, probably because my script on identifying the player is wrong, and therefore it is not firing the bindable event.
Here are my scripts (the bindable event is at the bottom of the sword script)

--Rescripted by Luckymaxer
--EUROCOW WAS HERE BECAUSE I MADE THE PARTICLES AND THEREFORE THIS ENTIRE SWORD PRETTY AND LOOK PRETTY WORDS AND I'D LIKE TO DEDICATE THIS TO MY FRIENDS AND HI LUCKYMAXER PLS FIX SFOTH SWORDS TY LOVE Y'ALl
--Updated for R15 avatars by StarWars
--Re-updated by TakeoHonorable

Tool = script.Parent
Handle = Tool:WaitForChild("Handle")

local damageModule = require(game.ServerStorage.DamageModule)
local BindableEvent = game.Workspace:WaitForChild("BindableEvent")

function Create(ty)
	return function(data)
		local obj = Instance.new(ty)
		for k, v in pairs(data) do
			if type(k) == 'number' then
				v.Parent = obj
			else
				obj[k] = v
			end
		end
		return obj
	end
end

local BaseUrl = "rbxassetid://"

Players = game:GetService("Players")
Debris = game:GetService("Debris")
RunService = game:GetService("RunService")

DamageValues = {
	BaseDamage = 5,
	SlashDamage = 10,
	LungeDamage = 30
}

--For R15 avatars
Animations = {
	R15Slash = 522635514,
	R15Lunge = 522638767
}

Damage = DamageValues.BaseDamage

Grips = {
	Up = CFrame.new(0, 0, -1.70000005, 0, 0, 1, 1, 0, 0, 0, 1, 0),
	Out = CFrame.new(0, 0, -1.70000005, 0, 1, 0, 1, -0, 0, 0, 0, -1)
}

Sounds = {
	Slash = Handle:WaitForChild("SwordSlash"),
	Lunge = Handle:WaitForChild("SwordLunge"),
	Unsheath = Handle:WaitForChild("Unsheath")
}

ToolEquipped = false

--For Omega Rainbow Katana thumbnail to display a lot of particles.
for i, v in pairs(Handle:GetChildren()) do
	if v:IsA("ParticleEmitter") then
		v.Rate = 20
	end
end

Tool.Grip = Grips.Up
Tool.Enabled = true

function IsTeamMate(Player1, Player2)
	return (Player1 and Player2 and not Player1.Neutral and not Player2.Neutral and Player1.TeamColor == Player2.TeamColor)
end

function TagHumanoid(humanoid, player)
	local Creator_Tag = Instance.new("ObjectValue")
	Creator_Tag.Name = "creator"
	Creator_Tag.Value = player
	Debris:AddItem(Creator_Tag, 2)
	Creator_Tag.Parent = humanoid
end

function UntagHumanoid(humanoid)
	for i, v in pairs(humanoid:GetChildren()) do
		if v:IsA("ObjectValue") and v.Name == "creator" then
			v:Destroy()
		end
	end
end

function Blow(Hit)
	if not Hit or not Hit.Parent or not CheckIfAlive() or not ToolEquipped then
		return
	end
	local RightArm = Character:FindFirstChild("Right Arm") or Character:FindFirstChild("RightHand")
	if not RightArm then
		return
	end
	local RightGrip = RightArm:FindFirstChild("RightGrip")
	if not RightGrip or (RightGrip.Part0 ~= Handle and RightGrip.Part1 ~= Handle) then
		return
	end
	local character = Hit.Parent
	if character == Character then
		return
	end
	local humanoid = character:FindFirstChildOfClass("Humanoid")
	if not humanoid or humanoid.Health == 0 then
		return
	end
	local player = Players:GetPlayerFromCharacter(character)
	if player and (player == Player or IsTeamMate(Player, player)) then
		return
	end
	damageModule(humanoid,Player,Damage)
end


function Attack()
	Damage = DamageValues.SlashDamage
	Sounds.Slash:Play()

	if Humanoid then
		if Humanoid.RigType == Enum.HumanoidRigType.R6 then
			local Anim = Instance.new("StringValue")
			Anim.Name = "toolanim"
			Anim.Value = "Slash"
			Anim.Parent = Tool
		elseif Humanoid.RigType == Enum.HumanoidRigType.R15 then
			local Anim = Tool:FindFirstChild("R15Slash")
			if Anim then
				local Track = Humanoid:LoadAnimation(Anim)
				Track:Play(0)
			end
		end
	end	
end

function Lunge()
	Damage = DamageValues.LungeDamage

	Sounds.Lunge:Play()
	
	if Humanoid then
		if Humanoid.RigType == Enum.HumanoidRigType.R6 then
			local Anim = Instance.new("StringValue")
			Anim.Name = "toolanim"
			Anim.Value = "Lunge"
			Anim.Parent = Tool
		elseif Humanoid.RigType == Enum.HumanoidRigType.R15 then
			local Anim = Tool:FindFirstChild("R15Lunge")
			if Anim then
				local Track = Humanoid:LoadAnimation(Anim)
				Track:Play(0)
			end
		end
	end	
	--[[
	if CheckIfAlive() then
		local Force = Instance.new("BodyVelocity")
		Force.velocity = Vector3.new(0, 10, 0) 
		Force.maxForce = Vector3.new(0, 4000, 0)
		Debris:AddItem(Force, 0.4)
		Force.Parent = Torso
	end
	]]
	
	wait(0.2)
	Tool.Grip = Grips.Out
	wait(0.6)
	Tool.Grip = Grips.Up

	Damage = DamageValues.SlashDamage
end

Tool.Enabled = true
LastAttack = 0

function Activated()
	if not Tool.Enabled or not ToolEquipped or not CheckIfAlive() then
		return
	end
	Tool.Enabled = false
	local Tick = RunService.Stepped:wait()
	if (Tick - LastAttack < 0.2) then
		Lunge()
	else
		Attack()
	end
	LastAttack = Tick
	--wait(0.5)
	Damage = DamageValues.BaseDamage
	local SlashAnim = (Tool:FindFirstChild("R15Slash") or Create("Animation"){
		Name = "R15Slash",
		AnimationId = BaseUrl .. Animations.R15Slash,
		Parent = Tool
	})
	
	local LungeAnim = (Tool:FindFirstChild("R15Lunge") or Create("Animation"){
		Name = "R15Lunge",
		AnimationId = BaseUrl .. Animations.R15Lunge,
		Parent = Tool
	})
	Tool.Enabled = true
end

function CheckIfAlive()
	return (((Player and Player.Parent and Character and Character.Parent and Humanoid and Humanoid.Parent and Humanoid.Health > 0 and Torso and Torso.Parent) and true) or false)
end

function Equipped()
	Character = Tool.Parent
	Player = Players:GetPlayerFromCharacter(Character)
	Humanoid = Character:FindFirstChildOfClass("Humanoid")
	Torso = Character:FindFirstChild("Torso") or Character:FindFirstChild("HumanoidRootPart")
	if not CheckIfAlive() then
		return
	end
	ToolEquipped = true
	Sounds.Unsheath:Play()
end

function Unequipped()
	Tool.Grip = Grips.Up
	ToolEquipped = false
end

Tool.Activated:Connect(Activated)
Tool.Equipped:Connect(Equipped)
Tool.Unequipped:Connect(Unequipped)

Connection = Handle.Touched:Connect(Blow)

game.Workspace.Arena1Player1.Touched:Connect(function(touchedPart)
	local player1 = Players:GetPlayerFromCharacter(touchedPart.Parent)
	if player1.Humanoid.Health == 0 then
		print("Detected Health")
		BindableEvent:Fire()
	end
end)

The listening script:

local Players = game:GetService("Players")
local player = game.Players.LocalPlayer

local part1 = workspace.Arena1Player1
local part2 = workspace.Arena1Player2

local team1 = workspace.Arena1Team1
local team2 = workspace.Arena1Team2

local standingOnArena1Player1 = nil
local standingOnArena1Player2 = nil


local RemoteEvent = game.ReplicatedStorage:FindFirstChild("RemoteEvent")
local BindableEvent = game.Workspace:WaitForChild("BindableEvent")


local function startEvent()
	
	local plr1CurrentKills = 0
	local plr2CurrentKills = 0
	
	local SwordGiver = game.ServerStorage.Sword
	

	local Sword1 = SwordGiver:Clone()
	local char1 = standingOnArena1Player1.Character
	
	
	if char1 then
		local humanoid1 = char1:FindFirstChild("Humanoid")

		if humanoid1 then
			
			---- this if statement is working
			
			BindableEvent.Event:Connect(function()
				
				---- not working
				
				print("Working")
			end)
					Sword1.Parent = standingOnArena1Player1.Backpack
					humanoid1:EquipTool(Sword1)
		end
	end		

	local Sword2 = SwordGiver:Clone()
	local char2 = standingOnArena1Player2.Character
	if char2 then
		local humanoid2 = char2:FindFirstChild("Humanoid")
		
			if humanoid2 then
				Sword2.Parent = standingOnArena1Player2.Backpack
				humanoid2:EquipTool(Sword2)
			end		
	end
end

RemoteEvent.OnServerEvent:Connect(function() -- if any player presses the play button, executes event
	if standingOnArena1Player2 ~= nil and standingOnArena1Player1 ~= nil then
		--Hide playbutton gui
		standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = false
		standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = false
		--Teleports
		workspace[standingOnArena1Player1.Name].HumanoidRootPart.CFrame = CFrame.new(team1.Position.X,team1.Position.Y,team1.Position.Z)
		workspace[standingOnArena1Player1.Name].HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(90), 0)
		workspace[standingOnArena1Player2.Name].HumanoidRootPart.CFrame = CFrame.new(team2.Position.X,team2.Position.Y,team2.Position.Z)
		workspace[standingOnArena1Player2.Name].HumanoidRootPart.CFrame *= CFrame.Angles(0, math.rad(270), 0)
		local gameRunning = true
		startEvent() -- triggers function
	end
end)


-----------------------------------------------------

--				PART1 Events				 --

-----------------------------------------------------

-- Touched functions [Events now set each part that was touched to the players name instead of the opposite button]

part1.Touched:Connect(function(TouchedPart) -- Touched

	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)

	if Player then -- Check if it found character
		if standingOnArena1Player1 == nil then -- Check if not standing and if not occupied

			part1.BrickColor = BrickColor.new("Teal") -- Set color
			standingOnArena1Player1 = Player
		end	
	end
end)

part1.TouchEnded:Connect(function(TouchedPart)

	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)

	if Player then -- Check if it found character
		if standingOnArena1Player1 ~= nil then -- Check if standing and is occupied
			if  standingOnArena1Player1.PlayerGui.ScreenGui.Enabled == true then
				standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = false
			end
			part1.BrickColor = BrickColor.new("Smoky grey") -- Set color
			standingOnArena1Player1 = nil

			if standingOnArena1Player2 ~= nil then
				if standingOnArena1Player2.PlayerGui.ScreenGui.Enabled == true then
					standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = false
				end
			end
		end	
	end
end)

-----------------------------------------------------

--				PART2 Events				 --

-----------------------------------------------------

part2.Touched:Connect(function(TouchedPart) -- Touched

	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)

	if Player then -- Check if it found character
		if standingOnArena1Player2 == nil then -- Check if not standing and if not occupied

			part2.BrickColor = BrickColor.new("Teal") -- Set color
			standingOnArena1Player2 = Player
		end	
	end
end)

part2.TouchEnded:Connect(function(TouchedPart)

	local Player = Players:GetPlayerFromCharacter(TouchedPart.Parent)

	if Player then -- Check if it found character
		if standingOnArena1Player2 ~= nil then -- Check if standing and is occupied
			if   standingOnArena1Player2.PlayerGui.ScreenGui.Enabled == true then
				standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = false
			end
			part2.BrickColor = BrickColor.new("Smoky grey") -- Set color
			standingOnArena1Player2 = nil

			if standingOnArena1Player1 ~= nil then
				if  standingOnArena1Player1.PlayerGui.ScreenGui.Enabled == true then
					standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = false
				end
			end
		end	
	end
end)

while task.wait(0) do
	if standingOnArena1Player2 ~= nil and standingOnArena1Player1 ~= nil then -- checks if both players are on top
		standingOnArena1Player2.PlayerGui.ScreenGui.Enabled = true
		standingOnArena1Player1.PlayerGui.ScreenGui.Enabled = true
	end
end

if you are using a sword or any other tool, you don’t want to do the damage and check for death on the client, because this can be exploited.

the client only needs to tell the server (through a remote event) (or with the Tool.Activated event) when the player has clicked the button to attack.

when the server script gets this event, have it see if the sword hits a player, if it does, place a tag on the player (playerWhoGotHit:SetAttribute(“LastAttacker”,playerWhoWasAttacking.UserId)

have a serve script that detects when a player dies, and check their health, to make sure it is <=0 (because they might have just clicked the reset character button)

when you detect a death see who was the last player to hit the dead player
killerId = playerWhoDied:GetAttribute(“LastAttacker”)

2 Likes

Thank you very much for the response, sorry I’m struggling to understand what to do as this is all way above my level of scripting, is there a simpler way I can do this, i’m not too worried about the exploiting side of things, because by the time I get the scripting all done, I should be better and can change my script to be safer from exploiters. For now, I just wish to get my game functional

You can maybe make a script that looks like this?

game.Players.PlayerAdded:Connect(function(plr)
   plr.CharacterAdded:Connect(function()
      plr.Character.Humanoid.Died:Connect(function(tag)
         print("dead")
      end)
   end)
end)

Keep in mind, I don’t think you can just slap this code into your script. This is just an example of something you COULD do.

1 Like
humanoid1:GetPropertyChangedSignal("Health"):Connect(function()
if humanoid1.Health <= 0 then
-- do whatever you need to do in here
end
end)

If this doesn’t work, then maybe your method of damaging the players is not working.

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