So I have a server script that goes in the npc that’s suppose to make it attack the player every little bit, but for some reason the npc will only throw one punch and no more without any code errors. I’ve tried different ways of playing the punch animation but that didn’t work.
Code:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")
local Debris = game:GetService("Debris")
local function TagHumanoid(humanoid, player)
local Creator_Tag = Instance.new("ObjectValue")
Creator_Tag.Name = "creator"
Creator_Tag.Value = player
Debris:AddItem(Creator_Tag, 2)
Creator_Tag.Parent = humanoid
end
local function UntagHumanoid(humanoid)
for _, v in pairs(humanoid:GetChildren()) do
if v:IsA("ObjectValue") and v.Name == "creator" then
v:Destroy()
end
end
end
local function GetNearestPlayer(position, maxDistance)
local players = game:GetService("Players"):GetPlayers()
local nearestPlayer
local shortestDistance = maxDistance
for _, player in pairs(players) do
local character = player.Character
if character and character:FindFirstChild("Humanoid") then
local humanoid = character:WaitForChild("Humanoid")
local distance = (position - humanoid.Parent.PrimaryPart.Position).Magnitude
if distance < shortestDistance then
nearestPlayer = player
shortestDistance = distance
end
end
end
return nearestPlayer
end
local function ThrowPunch(npc, actionName, animationId, damage, swingAudioName)
local Remote = ReplicatedStorage:WaitForChild(actionName .. "Remote")
local Action = ReplicatedStorage:WaitForChild(actionName)
local Animation = Instance.new("Animation")
Animation.AnimationId = animationId
local function PlayPunch()
local humanoid = npc:WaitForChild("Humanoid")
local position = humanoid.Parent.PrimaryPart.Position
local nearestPlayer = GetNearestPlayer(position, 5)
if nearestPlayer then
-- Load and play animation
local AnimationTrack = humanoid:LoadAnimation(Animation)
AnimationTrack:Play()
-- Remote event call
Remote:InvokeClient(nearestPlayer)
-- Wait for a break before the next punch
wait(math.random(1, 6))
end
end
while wait(1) do
PlayPunch()
end
end
-- NPC reference
local npc = script.Parent -- Assume this script is placed in the NPC model
-- Setup left punch with Swing2 audio
ThrowPunch(npc, "LeftPunch", "rbxassetid://15479380094", 10, "Swing2")
-- Setup right punch with Swing1 audio
ThrowPunch(npc, "RightPunch", "rbxassetid://15476721938", 8, "Swing1")