Why is not the function running?

So the problem is that i cant get the CastRock() function to run. im not a good scripter so there might be an issue somewhere.

local Zombie = script.Parent
local Damage = 10


function FindPlayer(Position)
	local List = game.Workspace:GetChildren()
	local Torso = nil
	local Distance = 100
	local HumanoidRootPart = nil
	local Humanoid = nil
	local Player = nil

	for i = 1, #List do
		Player = List[i]
		if Player.ClassName == "Model" and Player ~= script.Parent and Player:FindFirstChild("HumanoidRootPart") and Player:FindFirstChild("Humanoid") then
			local isZombie = Player:FindFirstChild("IsZombie")
			if not isZombie then
				HumanoidRootPart = Player:FindFirstChild("HumanoidRootPart")
				Humanoid = Player:FindFirstChild("Humanoid")
				if Humanoid.Health > 0 and (HumanoidRootPart.Position - Position).Magnitude < Distance then
					Torso = HumanoidRootPart
					Distance = (HumanoidRootPart.Position - Position).Magnitude
				end
			end
		end
	end
	return Torso
end


local humanoid = Zombie:WaitForChild("Humanoid")
local walkAnimation = Instance.new("Animation")
walkAnimation.AnimationId = "rbxassetid://15625825209"
local loadedWalkAnimation = humanoid:LoadAnimation(walkAnimation)

while true do
	wait(1)
	local sound = Zombie.Head.Sound
	sound:Play()

	local Target = FindPlayer(script.Parent.HumanoidRootPart.Position)
	if Target then
		script.Parent.Humanoid:MoveTo(Target.Position, Target)
		script.Parent.Humanoid.WalkSpeed = 10
		
		local function CastRock()
			print("Castrockfunction")
			local sound = script.Parent.Head.HitSound
			local rock = game.ServerStorage.Rockyrock
			local direction = script.Parent.HumanoidRootPart.CFrame.LookVector
			local forceMultipliter = 100 * rock:GetMass()
			local newrock = rock:Clone()
			newrock.Parent = workspace
			newrock.Position = Zombie.Torso.Position
			newrock:ApplyImpulse(direction * forceMultipliter)
			
			wait(3)
			newrock:Destroy()
			print("rock thrown")
			
		end

		loadedWalkAnimation:Play()
		print("Animation playing")
		wait(loadedWalkAnimation)


		local targetHumanoid = Target:FindFirstChild("Humanoid")
		local db = false
		local dbtime = 10
		if targetHumanoid and (Target.Position - Zombie.HumanoidRootPart.Position).Magnitude < 20 and not db then
			loadedWalkAnimation:Stop()
			sound:Stop()
			db = true
			CastRock()
			print("Castrock")
			loadedWalkAnimation:Play()
			sound:Play()
			targetHumanoid:TakeDamage(Damage)
			wait(dbtime)
			db = false
			end

			wait(2)
			print("success")
		end
	end
		

3 Likes

Because you are not calling the function

3 Likes

He is calling the function CastRock()

2 Likes

I think the problem is because the function is being declared inside the while loop

3 Likes

Separate all user-defined functions from any loop with no condition.

3 Likes

i think i have separated them now but it didnt make any difference. The script runs normally but its just not printing anything related to that function.

2 Likes

try printing this before u do the if check oh and also idk if u just didn’t copy correctly but there is an end missing never mind, there isn’t one missing

print(targetHumanoid, (Target.Position - Zombie.HumanoidRootPart.Position).Magnitude, not db)

put that before u do the if check in the loop that would help alot

2 Likes

its printing “nil 71.80401611328125 true”

1 Like

its not printing “hello world” after that so i think that something is wrong with my positions?

1 Like

im pretty sure that something is wrong with the targetHumanoid variable for it to be printing nil

are you sure it exists at the time the function is being called?

edit: ok so i tried looking at the code and it appears that targetHumanoid tries to get a humanoid object inside a humanoidrootpart (Target), which (correct me if im wrong please), is the Torso variable returned from the FindPlayer() function. maybe this has something to do with the issue, because if i remember correctly, (correct me if im incorrect again) the humanoid object isnt normally inside the HumanoidRootPart of a character, which is why its printing nil

i apologize if this isnt very readable

After each block segment make a print in the FindPlayer Function and see if it prints something

could you try changing the line to

targetHumanoid = Target.Parent:FindFirstChild("Humanoid")

Expected ‘:’ not ‘.’ calling member function FindFirstChild

im bit confused. But yeah i changed it

Could you say what exactly List is? Since its an array of the Workspace’s children I have the feeling that a character model might not be included in that.

oops! sorry i had a typo in the original message

ok it should be fixed now

Thanks it was that what caused it to not work!

no problem!

good luck on your game

1 Like

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