Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

i am unsure how to make my script select the correct zombie npc, can you help?

i have tried using GetChildren however it doesn't work

local serverstorage = game:GetService("ServerStorage")
local replicatedstorage = game:GetService("ReplicatedStorage")
local zombie =  serverstorage:WaitForChild("zombie")
local pl = game:GetService("Players")
local p = pl:GetPlayers()

while wait(1) do
	if #game.Workspace.zombies:GetChildren() == 0 then
		for i =0 , _G.dead, 1 do
			local zom = zombie:Clone()
			local me = game.Workspace[tostring(_G.pl[math.random(1,#_G.pl)])]
			
			zom.Parent = game.Workspace.zombies
			zom:SetPrimaryPartCFrame(me.HumanoidRootPart.CFrame * CFrame.new(math.random(1,100),10,math.random(1,100)))
			local npc = game.Workspace.zombies:WaitForChild("zombie")
			local RunAnim = npc.Zombie.runanim

			local loadrun = npc.Zombie:LoadAnimation(RunAnim)

			
			loadrun:Play()
			
			
		end
	end
end 



how can i make it so that the animation plays on the zombie that i just spawned not the first in the folder?

2 answers

✓ Accepted answer

Your newly spawned zombie is stored in the zom variable, load animation on it.

local zom = zombie:Clone()
local me = game.Workspace[tostring(_G.pl[math.random(1,#_G.pl)])]

zom.Parent = game.Workspace.zombies
zom:SetPrimaryPartCFrame(me.HumanoidRootPart.CFrame * CFrame.new(math.random(1,100),10,math.random(1,100)))
--local npc = game.Workspace.zombies:WaitForChild("zombie")
local RunAnim = zom.Zombie.runanim

--local loadrun = zom.Zombie:LoadAnimation(RunAnim)
local loadrun = zom.Zombie.Animator:LoadAnimation(RunAnim)


loadrun:Play()

Note that I used zom.Zombie.Animator:LoadAnimation() instead of zom.Zombie:LoadAnimation(), this is because LoadAnimation on Humanoid is deprecated and should not be used for new work, see this post for details.

i am so great full to imKirda, you have fixed my problem with only 2 lines of code i truly appreciate it

This discussion is preserved as it was. New answers, comments, and votes are closed.