Issue with my script

So this script basically just spawns a zombie model at one of my spawn points that I designated. But for some reason it’s giving me an error on this line:

	local f = workspace.TownCinematicParts.SpawnerZombie.Model[math.random(1,#spawner)]

This is what the workspace view looks like: https://gyazo.com/fd8907e39b492af25fe98bbb883f39ec

function spawnZombie(spawnPos, destination)
local RE = game:GetService("ReplicatedStorage")
local Zombies = RE:WaitForChild("Zombies").IntroCinematicZombies:GetChildren()
local Zombie = Zombies[math.random(1,#Zombies)]:Clone()
Zombie.Parent = workspace
Zombie:MoveTo(spawnPos.Position + Vector3.new(math.random(1,35),5,0))
local Debris = game:GetService("Debris")
Debris:AddItem(Zombie, 30)
end

local spawner = workspace.TownCinematicParts.SpawnerZombie.Model:GetChildren()
local destination = workspace.TownCinematicParts.Destination.Model:GetChildren()
	local f = workspace.TownCinematicParts.SpawnerZombie.Model[math.random(1,#spawner)]
	local d = workspace.TownCinematicParts.Destination.Model[math.random(1,#destination)]
	spawnZombie(f, d)

This is the error message: https://gyazo.com/cbf82c180b1d4ff1a80431a3824fc383

I’m not sure what I’m doing wrong here? It’s like it’s trying to index by name and not by child?

I don’t understand? I’m trying to choose a part where I get a child of the model. All 3 parts are inside the model, I’m just trying to index 1 of the parts if that makes sense?

If the variable f is supposed to be a different zombie model, or a specific part, either way the method you’re attempting to use is trying to call a number as the name of the child you’re accessing. One option you can use is at the beginning of the script, loop through each children of workspace.TownCinematicParts.SpawnerZombie.Model adding them to a indexed table, then from there, reference that table using your random method as before.

something like…

local modelChildren = {}
for _, child in pairs(workspace.TownCinematicParts.SpawnerZombie.Model:GetChildren()) do
modelChildren[#modelChildren+1] = child
end

and then

local f = modelChildren[math.random(1,#spawner)]

just be sure that all of the children have loaded before running your for loop

1 Like