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
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.
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
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
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.