My Rock Monster is not Working

I want a rock monster to follow the player when they enter a certain area and damage the player when the monster touches the player. Kinda like zombies in zombie attack but with a rock instead. I spent an hour trying to figure out why this error is happening:

Somebody please help, here’s the script:

local char = script.Parent
local humanoid = char:WaitForChild("Humanoid")
local hrp = char:WaitForChild("HumanoidRootPart")

local walkAnim = script:WaitForChild("Walk")
local walkTrack = humanoid.Animator:LoadAnimation(walkAnim)

local dmgAnim = script:WaitForChild("Damage")
local dmgTrack = humanoid.Animator:LoadAnimation(dmgAnim)

local targetPlayer
local dmgAmt = 10

local RunService = game:GetService("RunService")

local triggerPart = workspace.StarterD.Park.Base.RockSpawn

humanoid.Running:Connect(function(speed)
	if speed > 0 then
		if not walkTrack.IsPlaying then
			walkTrack:Play()
		end
	else
		if walkTrack.IsPlaying then
			walkTrack:Stop()
		end
	end
end)

function findNearestPlayer()
	local closestDistance = math.huge
	local touchingParts = triggerPart:GetTouchingParts()
	local plrTouch
	for i, player in pairs(game.Players:GetPlayers()) do
		for i, v in pairs(touchingParts) do
			if game.Players:GetPlayerFromCharacter(v.Parent) then
				plrTouch = true
			end
		end
		local distance = (hrp.Position - player.Character.HumanoidRootPart.Position).Magnitude
		if distance < closestDistance and plrTouch then
			closestDistance = distance
			targetPlayer = player
		end
	end
end

function damagePlayer()
	if targetPlayer then
		local playerHumanoid = targetPlayer.Character:FindFirstChildOfClass("Humanoid")
		if playerHumanoid then
			playerHumanoid:TakeDamage(dmgAmt)
			dmgTrack:Play()
		end
	end
end

RunService.Heartbeat:Connect(function()
	findNearestPlayer()
	if targetPlayer then
		humanoid:MoveTo(targetPlayer.Character.HumanoidRootPart.Position)
		if (hrp.Position - targetPlayer.Character.HumanoidRootPart.Position).Magnitude <= 3 then
			damagePlayer()
		end 
	end
end)

humanoid.Died:Connect(function()
	local newChar = char:Clone()
	newChar.Parent = char.Parent
	char:Destroy()
end)

The script is a little long, hope you dont mind :sweat_smile:

1 Like

Try to replace player.Character.HumanoidRootPart.Position to player.Character.PrimaryPart.Position,
The primary part of the character should be the HumanoidRootPart.

Also, check if the character exists by adding an if condtion.

if player.Character then
    --Your Script...
end
1 Like

Now the rock just stays there and doesnt follow the player

1 Like

Instead of :

try :

local touchingParts = game:GetService("Workspace"):GetPartsInPart(triggerPart)

Nope, still doesn’t work, Should I remove the if plr.Character then that you earlier mentioned?

No need to remove the if plr.Character then, Can you show me what the console says now?

It doesn’t give any output from the script. I dont think there is a plr.Character because there is no error but the rock isn’t following the player.

Update: I used a few print statements and the entire code is working fine but there seems to be a problem with this one line
local distance = (hrp.Position - player.Character:WaitForChild("HumanoidRootPart").Position).Magnitude

You may try :

local hrp = char.PrimaryPart

and replace again :

local distance = (hrp.Position - player.Character.PrimaryPart.Position).Magnitude 
-- Instead of "local distance = (hrp.Position - player.Character:WaitForChild("HumanoidRootPart").Position).Magnitude"

The error says Attempt to index nil with WaitForChild()

Still doesn’t work, no error

Check your Rock Model and make sure that it has a Primary Part, you can set it through the properties :
image
Note that the primary part should be in the model…

I already checked and yes, it has a primary part and the all the parts except the HumanoidRootPart are not anchored. I also tried using print statements near the .Hearbeat function and I think there’s a problem with humanoid:MoveTo(targetPlayer.Character.HumanoidRootPart.Position)

Oh, because you should not anchor the HumanoidRootPart…

Oh, It works! Thanks! Although the tutorial I used for making custom rigs said you should anchor the HumanoidRootPart :roll_eyes:

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.