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)