Kalananti Game Coding Bootcamp - Sword Fighting Game (10-12)

Source Code

FollowingScript
local Players = game:GetService("Players")
local MaxDistance = 100

local Zombie = script.Parent
local ZombieHumanoid = Zombie.Humanoid
local ZombieRoot = Zombie.HumanoidRootPart

local function FindTarget()
	local Target = nil
	
	for Index, Character in pairs(game.Workspace:GetChildren()) do
		if Players:GetPlayerFromCharacter(Character) then
			local CharacterRoot = Character.HumanoidRootPart			
			local RootDistance = CharacterRoot.Position - ZombieRoot.Position
			
			if RootDistance.Magnitude < MaxDistance then
				Target = CharacterRoot
			end
		end
	end
	
	return Target
end

while wait() do
	local PlayerTarget = FindTarget()
	if PlayerTarget then
		ZombieHumanoid:MoveTo(PlayerTarget.Position)
	end
end
DamageScript
local Players = game:GetService("Players")

local Zombie = script.Parent
local ZombieHumanoid = Zombie.Humanoid

local ZombieLeftHand = Zombie.LeftHand
local ZombieRightHand = Zombie.RightHand

ZombieLeftHand.Touched:Connect(function(HitPart)
	local Player = Players:GetPlayerFromCharacter(HitPart.Parent)
	
	if Player and ZombieHumanoid.Health > 0 then
		local Character = HitPart.Parent
		local Humanoid = Character:WaitForChild("Humanoid")
		
		Humanoid:TakeDamage(5)
	end
end)

ZombieRightHand.Touched:Connect(function(HitPart)
	local Player = Players:GetPlayerFromCharacter(HitPart.Parent)

	if Player and ZombieHumanoid.Health > 0 then
		local Character = HitPart.Parent
		local Humanoid = Character:WaitForChild("Humanoid")

		Humanoid:TakeDamage(5)
	end
end)
RespawnScript
local Zombie = script.Parent
local ZombieHumanoid = Zombie.Humanoid

local ZombieClone = Zombie:Clone()

ZombieHumanoid.Died:Connect(function()
	wait(3)
	
	local ZombieFolder = game.Workspace:FindFirstChild("Zombies")

	if ZombieFolder then
		ZombieClone.Parent = ZombieFolder
	end
	
	Zombie:Destroy()
end)
PlayerAdded
local Players = game:GetService("Players")
local ZombieFolder = game.Workspace.Zombies

Players.PlayerAdded:Connect(function(Player)
	local Leaderstats = Instance.new("Folder", Player)
	Leaderstats.Name = "leaderstats"

	local Kill = Instance.new("IntValue", Leaderstats)
	Kill.Name = "Kill"
	Kill.Value = 0

	local Death = Instance.new("IntValue", Leaderstats)
	Death.Name = "Death"
	Death.Value = 0

	Kill:GetPropertyChangedSignal("Value"):Connect(function()
		if Kill.Value >= 10 then
			ZombieFolder:Destroy()

			local PlayerGui = Player.PlayerGui
			local ScreenGui = PlayerGui.ScreenGui
			local TextLabel = ScreenGui.TextLabel	

			TextLabel.Position = UDim2.fromScale(0.5, 1.5)

			ScreenGui.Enabled = true

			TextLabel:TweenPosition(
				UDim2.fromScale(0.5, 0.5),
				Enum.EasingDirection.Out,
				Enum.EasingStyle.Back,
				0.5,
				false
			)
		end
	end)

	Player.CharacterAdded:Connect(function(Character)
		local Humanoid = Character:WaitForChild("Humanoid")

		Humanoid.Died:Connect(function()
			local Leaderstats = Player:WaitForChild("leaderstats")
			local Death = Leaderstats:WaitForChild("Death")

			Death.Value += 1
		end)
	end)
end)
SwordScript
local Tool = script.Parent
local Handle = Tool.Handle

local SlashSound = Handle.SwordSlash
local UnsheathSound = Handle.Unsheath

Tool.Activated:Connect(function()
	if Tool.Enabled == true then
		Tool.Enabled = false
		
		SlashSound:Play()
		
		local Character = Tool.Parent
		local Humanoid = Character:WaitForChild("Humanoid")
		local Animator = Humanoid:WaitForChild("Animator")

		local Animation = Instance.new("Animation")
		Animation.AnimationId = "rbxassetid://11625587210"

		local AnimationTrack = Animator:LoadAnimation(Animation)
		AnimationTrack:Play()
		
		AnimationTrack.Stopped:Wait()
		
		Tool.Enabled = true
	end
end)

Tool.Equipped:Connect(function()
	UnsheathSound:Play()
end)

Handle.Touched:Connect(function(HitPart)
	local Character = Tool.Parent
	local Player = game.Players:GetPlayerFromCharacter(Character)
	
	local ZombieCharacter = HitPart.Parent
	local ZombieHumanoid = ZombieCharacter:FindFirstChild("Humanoid")
	
	if ZombieHumanoid and Character ~= ZombieCharacter then
		if ZombieHumanoid.Health > 0 then
			ZombieHumanoid:TakeDamage(10)
			ZombieHumanoid:SetAttribute("KillerName", Player.Name)
		else
			local KillerName = ZombieHumanoid:GetAttribute("KillerName")
			local Killer = game.Players:FindFirstChild(KillerName)
			
			if Killer then
				ZombieHumanoid:SetAttribute("KillerName", "")
				local Leaderstats = Killer:WaitForChild("leaderstats")
				local Kill = Leaderstats:WaitForChild("Kill")
				
				Kill.Value += 1
			end
		end
	end
end)

Starter Project

Roblox - Sword Fighting Starter Project.rbxl (114.2 KB)