Invalid Argument #2 (String expected, got Instance)

I’ve been working on my TDS game when this error message got me.

My script :

local enemies = game:GetService("ReplicatedStorage").Monsters

local monster = {}

function monster.moveEnemy(enemy, map)
	
	if not map then
		
		map = workspace.Info.Map.Value
		
	end

	enemy:SetPrimaryPartCFrame(map.EnemyBase.CFrame)
	
	local nodes = map.EnemySpawns:GetChildren()[math.random(1, #map.EnemySpawns:GetChildren())] 
	
	local humanoid = enemy.Humanoid

	for node=1, #nodes:GetChildren() do
		if enemy.PrimaryPart == nil or enemy == nil then return end

		humanoid:MoveTo(nodes[nodes].Position)

		--[[local unitPosVector = enemy.PrimaryPart.Position * Vector3.new(1, 0, 1)
		local waypointPosVector = nodes[node].Position * Vector3.new(1, 0, 1)
		local unitDist = (unitPosVector - waypointPosVector).Magnitude

		local waypointVector = Vector3.new(nodes[node].Position.X, nodes[node].Position.Y, nodes[node].Position.Z)

		local reached = false

		repeat task.wait(0.05)

			if not humanoid or humanoid.Health <= 0 then return end

			humanoid:MoveTo(waypointPosVector)

			unitPosVector = enemy.PrimaryPart.Position * Vector3.new(1, 0, 1)
			waypointPosVector = nodes[node].Position * Vector3.new(1, 0, 1)
			unitDist = (unitPosVector - waypointPosVector).Magnitude

			if unitDist <= 0.7 then

				reached = true

			end

		until reached == true]]
	end

	map.PlayerBase.Humanoid.Health:TakeDamage(enemy.Humanoid.Health)
	enemy:Destroy()

end

function monster.Optimize(unitToOptimize)

	local humanoid = unitToOptimize:WaitForChild("Humanoid")

	if unitToOptimize:FindFirstChild("HumanoidRootPart") then
		unitToOptimize.HumanoidRootPart:SetNetworkOwner(nil)
	else
		unitToOptimize.PrimaryPart:SetNetworkOwner(nil)
	end

	if not humanoid then return end

	humanoid:SetStateEnabled(Enum.HumanoidStateType.Seated, false)
	humanoid:SetStateEnabled(Enum.HumanoidStateType.Jumping, false)
	humanoid:SetStateEnabled(Enum.HumanoidStateType.Running, false)
	humanoid:SetStateEnabled(Enum.HumanoidStateType.GettingUp, false)
	humanoid:SetStateEnabled(Enum.HumanoidStateType.Climbing, false)
	humanoid:SetStateEnabled(Enum.HumanoidStateType.Landed, false)
	humanoid:SetStateEnabled(Enum.HumanoidStateType.Ragdoll, false)
	humanoid:SetStateEnabled(Enum.HumanoidStateType.Freefall, false)

end

function monster.Spawn(enemy, amount, map)
	
	enemy = game:GetService('ReplicatedStorage').Monsters[enemy]
	
	local map = workspace.Info.Map.Value
	
	for i=1, amount do
		
		task.wait(0.5)
		local newMob = enemy:Clone()
		newMob.Parent = workspace.Monsters
		newMob.HumanoidRootPart:SetNetworkOwner(nil)
		
		monster.Optimize(newMob)
		
		newMob.Humanoid.MaxHealth = newMob.Humanoid.MaxHealth * #game.Players:GetPlayers()
		newMob.Humanoid.Health = newMob.Humanoid.Health * #game.Players:GetPlayers()
		
		newMob.Humanoid.Died:Connect(function()

			task.wait(0.5)
			newMob:Destroy()

		end)

		coroutine.wrap(monster.moveEnemy)(newMob, map)
		
	end
	
end

return monster

Which line did you get this error?

1 Like

the error was found on line 103 (sorry for replying late)

1 Like

Coroutines arent called by a set of brackets at the end like functions, instead pass them through in the first set of brackets like: coroutine.wrap(monster.moveEnemy, newMob, map)

1 Like

it does not work, it just stands there with no errors.

1 Like

If that is what the error is, then you are dealing with an object in the game, not a string of text. If you are trying to use the objects name to find something, just put .Name, and it should be resolved.

1 Like

please explain to me where i should do that…

At the line the error occurred at. If you were using FindFirstChild, (or some other Roblox method) the argument could be the issue (part inside parenthesis).

1 Like

which argument, argument 1 or 2?

The first one, if its FindFirstChild that is the problem. The second argument is to say if it should be recursive or not.

1 Like

it’s not argument 1, it spawns the mob, but it says 'invalid argument #2 (string expected, got Instance) it does the same thing if i convert it to text

can you send the code on the line that is erroring

1 Like

I fixed it, I had to tweak A LOT of things. Thank you to everyone to attempted to help! I will heart every reply!

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