local currentCamera: Camera = workspace.CurrentCamera
local Debris = game:GetService("Debris")
local player: Player = game:GetService("Players").LocalPlayer
local UIS: UserInputService = game:GetService("UserInputService")
local DAMAGE = 10
local character = player.Character or player.CharacterAdded:Wait()
local tool: Tool = player.Backpack.Tool
local raycastParams: RaycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = { character } -- fill this in yourself
local aiming = false
UIS.InputEnded:Connect(function(input)
local inputType = input.UserInputType
if inputType == Enum.UserInputType.MouseButton2 then
aiming = false
end
end)
UIS.InputBegan:Connect(function(input)
local inputType = input.UserInputType
if inputType == Enum.UserInputType.MouseButton2 then
aiming = true
end
end)
local MIN_BAD_AIM = 100
local MAX_BAD_AIM = 100
tool.Activated:Connect(function(input)
local muzzle = tool.Handle
print("button is down")
local VIEWPORT_SIZE = currentCamera.ViewportSize / 2 -- current size of the screen -- if you're raycasting multiple times, this will have to be redefined!
local CAST_LENGTH = 1000 -- distance to raycast
local x_offset = 0
local y_offset = 0
if not aiming then
x_offset += math.random(-MIN_BAD_AIM, MIN_BAD_AIM)
y_offset += math.random(-MIN_BAD_AIM, MIN_BAD_AIM)
end
local unitRay = currentCamera:ViewportPointToRay(VIEWPORT_SIZE.X + x_offset, VIEWPORT_SIZE.Y + y_offset) -- account for the 36px TopBar inset
local raycastResult = workspace:Raycast(unitRay.Origin, unitRay.Direction * CAST_LENGTH, raycastParams)
if raycastResult and raycastResult.Position then -- .Position is the Vector3 (the Hit.Position) of the center of the screen
local partFound: Instance = raycastResult.Instance
local distance = (muzzle.Position - raycastResult.Position).Magnitude
local p = Instance.new("Part")
p.Anchored = true
p.CanCollide = false
p.Parent = workspace
p.Size = Vector3.new(0.1, 0.1, distance)
p.CFrame = CFrame.lookAt(muzzle.Position, raycastResult.Position)*CFrame.new(0, 0, -distance/2)
task.defer(function()
local transparency = 0
while transparency ~= 1 do
p.Transparency += 0.1
task.wait(0.02)
end
Debris:AddItem(p, 0.05)
end)
local humanoid = partFound.Parent:WaitForChild("Humanoid", 5)
if humanoid then
humanoid.Health -= DAMAGE
end
end
end)