First Person Shooter (4-6)

LocalScript for Gun

--Object and Sound Variables

local gun = script.Parent

local gun_sound = gun.Handle["Gun Shot"]

local empty_sound = gun.Handle.clip_empty

local reload_sound = gun.Handle.Reload

local player = game.Players.LocalPlayer

local clipSize = gun:WaitForChild('Ammo').Value

local ammo = gun:WaitForChild('Ammo')



--UserInputService Setup

local userInput = game:GetService('UserInputService')


--Mouse Icon

local mouse = game.Players.LocalPlayer:GetMouse()


mouse.Icon = 'rbxassetid://117431027'


--Remote Event Setup


local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild('ShotEvent')


--Checks if Mouse is clicked


gun.Equipped:Connect(function(mouse)

player.PlayerGui.ScreenGui.Ammo.Visible = true

player.PlayerGui.ScreenGui.Ammo.Text = 'Ammo: ' .. tostring(ammo.Value) .. ' / ' .. tostring(gun.MaxAmmo.Value)

mouse.Button1Down:Connect(function()

if gun.Ammo.Value > 0 then

remoteEvent:FireServer(gun.Handle.Position, gun.Handle.Orientation, mouse.Hit.p)

gun_sound:Play()

gun.Ammo.Value = gun.Ammo.Value - 1

else

empty_sound:Play()

end

end)

mouse.Button2Down:Connect(function()

local camera = game.Workspace.CurrentCamera

camera.FieldOfView = 40

end)

mouse.Button2Up:Connect(function()

local camera = game.Workspace.CurrentCamera

camera.FieldOfView = 70

end)

end)


-- Unequip gun


gun.Unequipped:Connect(function()

player.PlayerGui.ScreenGui.Ammo.Visible = false

end)



--Checks if the letter R is pressed to reload


userInput.InputBegan:Connect(function(input, gameProcessed)

if not gameProcessed then

if input.UserInputType == Enum.UserInputType.Keyboard then

local keycode = input.KeyCode

if keycode == Enum.KeyCode.R then

if gun.Ammo.Value < clipSize and gun.MaxAmmo.Value > 0 then

reload_sound:Play()

reload_sound.Ended:Wait()

if gun.MaxAmmo.Value - (clipSize - gun.Ammo.Value) >= 0 then

gun.MaxAmmo.Value = gun.MaxAmmo.Value - (clipSize - gun.Ammo.Value)

gun.Ammo.Value = clipSize

else

gun.Ammo.Value = gun.Ammo.Value + gun.MaxAmmo.Value

gun.MaxAmmo.Value = 0

player.PlayerGui.ScreenGui.Ammo.Text = 'Ammo: ' .. tostring(ammo.Value) .. ' / ' .. tostring(gun.MaxAmmo.Value)

end

end

end

end

end

end)


-- Update ammo GUI


ammo.Changed:Connect(function()

player.PlayerGui.ScreenGui.Ammo.Text = 'Ammo: ' .. tostring(ammo.Value) .. ' / ' .. tostring(gun.MaxAmmo.Value)

end)


ServerStorage (Name: Damage)

local bullet = script.Parent


local function player_check(otherPart)

local humanoid = otherPart.Parent:FindFirstChild('Humanoid')

if humanoid and humanoid.Parent.Name ~= bullet.Attacker.Value then

humanoid:TakeDamage(30)

local player = game.Players:FindFirstChild(humanoid.Parent.Name)

if player then

local tag = player:FindFirstChild('Killer')

if tag then

tag.Value = bullet.Attacker.Value

bullet:Destroy()

end

end

end

end


bullet.Touched:Connect(player_check)

ServerScriptService (Name: Leaderboard)

local function onPlayerJoin(player)

local leaderstats = Instance.new("Folder")

leaderstats.Name = 'leaderstats'

leaderstats.Parent = player

local kills = Instance.new("IntValue")

kills.Value = 0

kills.Name = 'Kills'

kills.Parent = leaderstats

local deaths = Instance.new('IntValue')

deaths.Value = 0

deaths.Name = 'Deaths'

deaths.Parent = leaderstats

local killer = Instance.new('StringValue')

killer.Name = 'Killer'

killer.Parent = player


end


game.Players.PlayerAdded:Connect(onPlayerJoin)


--Death Tracker


game:GetService('Players').PlayerAdded:Connect(function(player)

player.CharacterAdded:Connect(function(character)

character:WaitForChild("Humanoid").Died:Connect(function()

player.leaderstats.Deaths.Value = player.leaderstats.Deaths.Value + 1

local killerTag = game.Players:FindFirstChild(player.Killer.Value)

if killerTag then

killerTag.leaderstats.Kills.Value = killerTag.leaderstats.Kills.Value + 1

end

end)

end)

end)

ServerScriptService (Name: BulletCreate)

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local remoteEvent = ReplicatedStorage:WaitForChild('ShotEvent')

local ServerStorage = game:GetService('ServerStorage')


remoteEvent.OnServerEvent:Connect(function(player, gunPos, gunOr, mosPos)

local bullet = Instance.new('Part')

bullet.Name = 'Bullet'

bullet.Parent = game.Workspace

bullet.Shape = Enum.PartType.Cylinder

bullet.Size = Vector3.new(.5,.25,.5)

bullet.BrickColor = BrickColor.new('Gold')

local attacker = Instance.new('StringValue')

attacker.Name = 'Attacker'

attacker.Parent = bullet

attacker.Value = player.Name

local damageScript = ServerStorage:FindFirstChild('Damage'):Clone()

damageScript.Parent = bullet

local speed = 500

bullet.CFrame = CFrame.new(gunPos, mosPos)

bullet.Velocity = bullet.CFrame.lookVector * speed

bullet.Orientation = gunOr + Vector3.new(0,-90,0)

game:GetService("Debris"):AddItem(bullet, 2)

end)



Health Text Label

local player = game.Players.LocalPlayer

local health_label = script.Parent


while wait() do

local percent = player.Character.Humanoid.Health / player.Character.Humanoid.MaxHealth

if percent < 0.1 then

health_label.BackgroundColor3 = Color3.new(255, 0 , 0)

end

if percent >= 0.1 and percent <= 0.75 then

health_label.BackgroundColor3 = Color3.new(100, 255, 0)

end

if percent > 0.75 then

health_label.BackgroundColor3 = Color3.new(0, 255, 0)

end

health_label.Size = UDim2.new(percent - 0.03, 0, 0.8, 0)

health_label.Text = math.floor(player.Character.Humanoid.Health)

end