Issue with ai script

function FindPlayer(zombie, player)
	local humanoid
	local radius = 100
	local distance
	local alerted = false

	for _, character in ipairs(workspace:GetChildren()) do
print("Working")
		if character:IsA("Model") and character:FindFirstChild('Humanoid') then
print("Work")
			local targetPlayer = players:GetPlayerFromCharacter(character)
			if targetPlayer then
				distance = (zombie.Torso.Position - character.HumanoidRootPart.Position).magnitude
				if distance < radius  then
					zombie.Zombie:MoveTo(character.HumanoidRootPart.Position)
				end
			end
		end
	end
end

Trying to script a move to player function however Im not getting any prints past this line

for _, character in ipairs(workspace:GetChildren()) do
print("Working")

Is there anything im doing wrong, also yes im using the function in another part of my script. Theres no errors being printed out

7 Likes

not sure but it might be: for _, v in ipairs(). ipairs was designed for an index, but since you’re using _, it wouldn’t work like that, try switching it to pairs

a few changes (minor):

function FindPlayer(zombie)
	local radius = 100
	local distance

	for _, character in workspace:GetChildren() do
print("Working")
		if character:FindFirstChild("Humanoid") then
print("Work")
			local targetPlayer = players:GetPlayerFromCharacter(character)
			if targetPlayer then
				distance = (zombie.PrimaryPart.Position - character.PrimaryPart.Position).Magnitude
				if distance < radius then
                   zombie:SetNetworkOwner(nil)
   				   zombie.Zombie:MoveTo(character.PrimaryPart.Position)
				end
			end
		end
	end
end
4 Likes

Yeah thats what i did at first and it didnt work so i switched it to ipairs

2 Likes

Also the print after that line before the character:Isa(“”) line works fine

3 Likes

image
Could it be because i didnt pass the names correctly

4 Likes

can’t be

abcdefghijklmnopleaseremove30characterule

3 Likes

try the new script
@FrancoBritishAI

3 Likes

i did no nothing happened,…]nmpk

4 Likes

made a few changes, show me output

3 Likes

3 Likes

fixed it iiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii

3 Likes

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