Automatic Gun
Local Script for Gun
local gun = script.Parent
local gun_shot = gun.Handle['Gun Shot']
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = ReplicatedStorage:WaitForChild('ShotEvent')
local shooting = false
local equipped = false
local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local mouseConnection
gun.Equipped:Connect(function()
equipped = true
mouse.Icon = 'rbxassetid://117431027'
mouseConnection = mouse.Button1Down:Connect(function()
shooting = true
while shooting and equipped do
remoteEvent:FireServer(gun.Handle.Position, gun.Handle.Orientation, mouse.Hit.p)
gun_shot:Play()
mouse.Button1Up:Connect(function()
shooting = false
end)
wait(0.1)
end
end)
end)
gun.Unequipped:Connect(function()
equipped = false
mouseConnection:Disconnect()
end)
Script (ServerScriptService)
local ReplicatedStorage = game:GetService('ReplicatedStorage')
local remoteEvent = ReplicatedStorage:WaitForChild('ShotEvent')
local damage_amount = 25
remoteEvent.OnServerEvent:Connect(function(player, gunPos, gunOr, mosPos)
local bullet = Instance.new('Part')
bullet.Name = 'Bullet'
bullet.Parent = game.Workspace
bullet.Shape = Enum.PartType.Ball
bullet.Size = Vector3.new(0.25, 0.25, 0.25)
bullet.BrickColor = BrickColor.new('Lime green')
local speed = 250
bullet.CFrame = CFrame.new(gunPos, mosPos)
bullet.Velocity = bullet.CFrame.lookVector * speed
bullet.Touched:Connect(function(otherPart)
local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
if humanoid and humanoid.Parent.Name ~= player.Name then
if humanoid.Health > 0 and humanoid.Health - damage_amount <= 0 then
player.leaderstats.Kills.Value += 1
end
humanoid:TakeDamage(damage_amount)
bullet:Destroy()
end
end)
game:GetService('Debris'):AddItem(bullet, 1)
end)
Script (ServerScriptService)
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local kills = Instance.new("IntValue")
kills.Name = "Kills"
kills.Parent = leaderstats
end)