So, I'm trying to make a script where whenever an NPC gets close enough to a player, then it starts attacking the player. I already have another script that moves the NPC around and makes it follow nearby players, but whenever I try to run this script, it prints the error:

" 21:40:54.543 - Unable to cast value to Object

21:40:54.544 - Stack Begin

21:40:54.544 - Script 'Workspace.Thug - Level 5.Attack', Line 64 - local punch

21:40:54.544 - Script 'Workspace.Thug - Level 5.Attack', Line 92

21:40:54.545 - Stack End"

Here's the script, by the way, it's a server script inside of the NPC:

local thug = script.Parent
local humanoid = thug.Humanoid

local howClose = 5

-- Animation Stuff
local punch1 = "rbxassetid://2219184536"
local punch2 = "rbxassetid://2219185342"
local punch3 = "rbxassetid://2219186257"
local punch4 = "rbxassetid://2219189003"

local punch1wait = 0.3
local punch2wait = 0.3
local punch3wait = 0.3
local punch4wait = 0.4

-- Replicated Storage
local repStorage = game:GetService("ReplicatedStorage")
local skills = repStorage:WaitForChild("Skills")

local punchingRemoteEvent = skills:WaitForChild("Punching")

-- Debounces
local db = false
local damage = false
local punching = false

-- Punch Function
local function punch(count)
	
	db = true
	local part
	local waitTime
	local punchAnim
	local countToChange
	
	if count == 1 then
		part = "LeftHand"
		waitTime = punch1wait
		punchAnim = punch1
		countToChange = 2
	elseif count == 2 then
		part = "RightHand"
		waitTime = punch2wait
		punchAnim = punch2
		countToChange = 3
	elseif count == 3 then
		part = "LeftFoot"
		waitTime = punch3wait
		punchAnim = punch3
		countToChange = 4
	elseif count == 4 then
		part = "RightFoot"
		waitTime = punch4wait
		punchAnim = punch4
		countToChange = 1
	end
	
	local anim = Instance.new("Animation")
	anim.AnimationId = punchAnim
	local punchanim = humanoid:LoadAnimation(anim)
	punchanim.Priority = Enum.AnimationPriority.Action
	punchanim:Play()
	punchingRemoteEvent:FireClient(part,punching)
	damage = true
	wait(waitTime)
	damage = false
	punchanim:Stop()
	count = countToChange
	db = false
	punching = false
	
end

wait(2)

-- Actual Punching
while true do
	
	for i,player in pairs(game.Players:GetChildren()) do
		
		local char = player.Character
		
		local distanceFrom = (thug.HumanoidRootPart.Position - char.HumanoidRootPart.Position).magnitude
		
		if distanceFrom <= howClose then
			
			local counter = 1
			
			if counter == 1 then
				
				punch(counter)
				
			elseif counter == 2 then
				
				punch(counter)
				
			elseif counter == 3 then
				
				punch(counter)
				
			elseif counter == 4 then
				
				punch(counter)
				
			end
			
		end
		
	end
	
	wait(0.4)
	
end

Another thing that has me confused is why does it say, "- local punch" in, "21:40:54.544 - Script 'Workspace.Thug - Level 5.Attack', Line 65 - local punch"? If you can find the issue, I would greatly appreciate a little help.

Edit: I figured it out, thank you to anyone who tried to help me anyway!

3 comments

0

why bother with the if statements from 90 to 106, they literally contain the same code

0

The first parameter of the FireClient has to be a player.

0

Yeah, I got it figured out, thanks though! I'll edit it so no one else replies thinking I'm still figuring it out.