local function Z()local Z=tick()if Z-timeOfPreviousShot<FIRE_RATE then return false end return true end local function e()local Z=UserInputService:GetMouseLocation()local e=workspace.CurrentCamera:ViewportPointToRay(Z.X,Z.Y)local Q=e.Direction*MAX_MOUSE_DISTANCE local I=workspace:Raycast(e.Origin,Q)if I then return I.Position else return e.Origin+Q end end local function Q()local Z=e()local Q=(Z-tool.Handle.Position).Unit local I=Q*MAX_LASER_DISTANCE local f=RaycastParams.new()f.FilterDescendantsInstances={Players.LocalPlayer.Character}local Y=workspace:Raycast(tool.Handle.Position,I,f)local W if Y then W=Y.Position local Z=Y.Instance:FindFirstAncestorOfClass("\077\111\100\101\108")if Z then local e=Z:FindFirstChild("\072\117\109\097\110\111\105\100")if e then eventsFolder.DamageCharacter:FireServer(Z,W)end end else W=tool.Handle.Position+I end timeOfPreviousShot=tick()eventsFolder.LaserFired:FireServer(W)LaserRenderer.createLaser(tool.Handle,W)end local function I()tool.Handle.Equip:Play()end local function f()if Z()then Q()end end tool.Equipped:Connect(I)tool.Activated:Connect(f)

local function canShootWeapon()
	local currentTime = tick()
	if currentTime - timeOfPreviousShot < FIRE_RATE then
		return false
	end
	return true
end

local function getWorldMousePosition()
	local mouseLocation = UserInputService:GetMouseLocation()

	-- Create a ray from the 2D mouse location
	local screenToWorldRay = workspace.CurrentCamera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)

	-- The unit direction vector of the ray multiplied by a maxiumum distance
	local directionVector = screenToWorldRay.Direction * MAX_MOUSE_DISTANCE

	-- Raycast from the roy's origin towards its direction
	local raycastResult = workspace:Raycast(screenToWorldRay.Origin, directionVector)

	if raycastResult then
		-- Return the 3D point of intersection
		return raycastResult.Position
	else
		-- No object was hit so calculate the position at the end of the ray
		return screenToWorldRay.Origin + directionVector
	end
end

local function fireWeapon()
	local mouseLocation = getWorldMousePosition()

	-- Calculate a normalised direction vector and multiply by laser distance
	local targetDirection = (mouseLocation - tool.Handle.Position).Unit

	-- The direction to fire the weapon, multiplied by a maximum distance
	local directionVector = targetDirection * MAX_LASER_DISTANCE

	-- Ignore the player's character to prevent them from damaging themselves
	local weaponRaycastParams = RaycastParams.new()
	weaponRaycastParams.FilterDescendantsInstances = {Players.LocalPlayer.Character}
	local weaponRaycastResult = workspace:Raycast(tool.Handle.Position, directionVector, weaponRaycastParams)

	-- Check if any objects were hit between the start and end position
	local hitPosition
	if weaponRaycastResult then
		hitPosition = weaponRaycastResult.Position

		-- The instance hit will be a child of a character model
		-- If a humanoid is found in the model then it's likely a player's character
		local characterModel = weaponRaycastResult.Instance:FindFirstAncestorOfClass("Model")
		if characterModel then
			local humanoid = characterModel:FindFirstChild("Humanoid")
			if humanoid then
				eventsFolder.DamageCharacter:FireServer(characterModel, hitPosition)
			end
		end
	else
		-- Calculate the end position based on maxiumum laser distance
		hitPosition = tool.Handle.Position + directionVector
	end

	timeOfPreviousShot = tick()

	eventsFolder.LaserFired:FireServer(hitPosition)
	LaserRenderer.createLaser(tool.Handle, hitPosition)
end

local function toolEquipped()
	tool.Handle.Equip:Play()
end

local function toolActivated()
	if canShootWeapon() then
		fireWeapon()
	end
end

tool.Equipped:Connect(toolEquipped)
tool.Activated:Connect(toolActivated)
