COMBAT
Raycast Gun
Paste as a Script inside a Tool. Click fires a ray from the character's head, spends one bullet, and reloads when the magazine is empty. Hits tag the attacker for Kill / Death Counter.
-- Paste as a Script inside a Tool.
local Players = game:GetService("Players")
local DAMAGE = 15
local MAGAZINE = 12
local RANGE = 400
local RELOAD_TIME = 1.6
local tool = script.Parent
local ammo = MAGAZINE
local reloading = false
local function markKiller(humanoid: Humanoid, player: Player)
local tag = humanoid:FindFirstChild("creator")
if not tag then
tag = Instance.new("ObjectValue")
tag.Name = "creator"
tag.Parent = humanoid
end
if tag:IsA("ObjectValue") then
tag.Value = player
end
end
local function reload()
if reloading then
return
end
reloading = true
task.wait(RELOAD_TIME)
ammo = MAGAZINE
reloading = false
end
tool.Activated:Connect(function()
local character = tool.Parent
if not character or not character:IsA("Model") or reloading then
return
end
if ammo <= 0 then
task.spawn(reload)
return
end
local head = character:FindFirstChild("Head")
if not head or not head:IsA("BasePart") then
return
end
ammo -= 1
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Exclude
params.FilterDescendantsInstances = { character }
local result = workspace:Raycast(head.Position, head.CFrame.LookVector * RANGE, params)
if result then
local humanoid = result.Instance.Parent and result.Instance.Parent:FindFirstChildOfClass("Humanoid")
if humanoid and humanoid.Health > 0 then
local player = Players:GetPlayerFromCharacter(character)
if player then
markKiller(humanoid, player)
end
humanoid:TakeDamage(DAMAGE)
end
end
if ammo <= 0 then
task.spawn(reload)
end
end)