Attack feature not working

Hello Everyone!

I was trying to make a fixed gear of this one broken gear roblox was making.

and it has this one attack feature where you click on the character/player and the dog goes after him to attack but its not working when I click on one of my test rigs but the spawning and moving works

but anyways I want to make it so it attacks the character you click on

local Spawned = false
local tool = script.Parent

local BarkSound = tool.Bark
local WowSound = tool.Wow

local Mode = "Not Spawned" -- Modes: Not Spawned, Idle, Follow, Attack

local AttackSpeed = 19.2
local Attacking = false

local function DestroyHandle()
	tool.Handle:Destroy()
end

local function Attack(player, SpawnedDoge, target)
	if not Spawned or Attacking or not target then return end
	local DogeHumanoid = SpawnedDoge:FindFirstChildOfClass("Humanoid")
	local enemyHumanoid = target:FindFirstChildOfClass("Humanoid")

	if enemyHumanoid and DogeHumanoid then
		Attacking = true
		Mode = "Attack"
		BarkSound:Play()

		spawn(function()
			while Attacking and target and target:FindFirstChild("HumanoidRootPart") do
				DogeHumanoid:MoveTo(target.HumanoidRootPart.Position)
				if (SpawnedDoge.HumanoidRootPart.Position - target.HumanoidRootPart.Position).magnitude < 3 then
					enemyHumanoid:TakeDamage(10)
					WowSound:Play()
					Attacking = false
				end
				wait(0.2)
			end
			Mode = "Idle"
		end)
	end
end

local function SpawnDoge(player)
	if Spawned then 
		warn("Doge already spawned!")
		return 
	end

	local DogeModule = require(87073935858366)
	game:GetService("LogService"):ClearOutput()
	DogeModule.Setup()

	local SpawnedDoge = game.ReplicatedStorage.Doge:Clone()
	SpawnedDoge.Parent = workspace
	SpawnedDoge.HumanoidRootPart.CFrame = player.Character.HumanoidRootPart.CFrame + Vector3.new(2, 0, 2)
	SpawnedDoge.Name = player.Name.."'s' Doge"

	local DogeHumanoid = SpawnedDoge:FindFirstChildOfClass("Humanoid")
	if DogeHumanoid then
		Mode = "Idle"
		spawn(function()
			while Spawned and player.Character and player.Character:FindFirstChild("HumanoidRootPart") do
				local playerPos = player.Character.HumanoidRootPart.Position
				local dogePos = SpawnedDoge.HumanoidRootPart.Position

				-- Check distance to decide mode
				if Mode ~= "Attack" then
					if (playerPos - dogePos).magnitude > 5 then
						Mode = "Follow"
						DogeHumanoid:MoveTo(playerPos + Vector3.new(2, 0, 2))
					else
						Mode = "Idle"
					end
				end
				wait(0.1)
			end
		end)
	end

	Spawned = true
end

local function OnPlayerClick(player, target)
	if Spawned then
		local SpawnedDoge = workspace:FindFirstChild(player.Name.." Doge")
		if SpawnedDoge then
			Attack(player, SpawnedDoge, target)
		end
	end
end

tool.Equipped:Connect(function()
	if not Spawned then
		DestroyHandle()
	else
		warn("Doge already spawned!")
	end
end)

tool.SpawnDoge.OnServerEvent:Connect(function(player)
	SpawnDoge(player)
end)
tool.Activated:Connect(OnPlayerClick)
1 Like

ignore my deleted post earlier

i noticed that you put a player parameter and a target parameter in Tool.Activated

however, Tool.Activated doesn’t have parameters at all. i suggest using the player’s mouse (which can be get via the parameters of Tool.Equipped or Player:GetMouse()) and using Mouse.Target to get the specific part the mouse is pointing at after activating the tool.

for the player parameter, just get the tool’s parent then get the player object by Players:GetPlayerFromCharacter()

it should look similar to this (at the end of the code):

local mouse
local Players = game:GetService("Players")

local function OnPlayerClick(player, target)
	if Spawned then
		local SpawnedDoge = workspace:FindFirstChild(player.Name.." Doge")
		if SpawnedDoge then
			Attack(player, SpawnedDoge, target)
		end
	end
end

tool.Equipped:Connect(function(toolMouse)
	if not Spawned then
		DestroyHandle()
	else
		warn("Doge already spawned!")
	end
	
	mouse = toolMouse
end)

tool.SpawnDoge.OnServerEvent:Connect(function(player)
	SpawnDoge(player)
end)

tool.Activated:Connect(function()
	if mouse ~= nil then
		local target = mouse.Target
		local targetCharacter = target:FindFirstAncestorOfClass("Model")
		
		if targetCharacter:FindFirstChildOfClass("Humanoid") then
			OnPlayerClick(Players:GetPlayerFromCharacter(tool.Parent), targetCharacter)
		end
	end
end)
1 Like

every time I click I get an error known as This Mouse is no longer active

1 Like

woops i just found out that the mouse only works in the client

try using the mouse in the client via a remote function, then get the target part in the client and send it to the server via the same remote function

(also i wont help now cuz i gotta sleep)

1 Like