Invalid argument #2 (string expected, got Instance)

im making a unit system for my tower defense game anyone can help?

image

task.wait(1.5)
local RunService = game:GetService("RunService") 
local PhysicsService = game:GetService("PhysicsService")
local ServerStorage = game:GetService("ServerStorage")
local TweenService = game:GetService("TweenService")
local animateZombieEvent = game:GetService("ReplicatedStorage").Events:WaitForChild("AnimateZombie")
local animateTowerEvent = game:GetService("ReplicatedStorage").Events:WaitForChild("AnimateTower")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local events = ReplicatedStorage:WaitForChild("Events")
local animateUnitEvent = events:WaitForChild("AnimateTower")
local Unit = {}

local function setAnimation(object, animName)
	local AnimationController = object:FindFirstChild("AnimationController")
	if AnimationController then
		local animationObject = AnimationController:FindFirstChild(animName)

		if animationObject then

			local playingTracks = AnimationController:GetPlayingAnimationTracks()

			for i, track in pairs(playingTracks) do
				if track.Name == animName then
					return track
				end
			end

			local animationTrack = AnimationController:LoadAnimation(animationObject)
			return animationTrack
		end
	end
end

function Unit.Move(newUnit, UnitConfig, Path)
	local Health = game["Workspace"]["Units"][newUnit][UnitConfig]:FindFirstChild("Health")
	local Map = workspace.Map:FindFirstChildOfClass("Folder")
	local config = game["Workspace"]["Units"][newUnit]:WaitForChild("Config", 0.5)
	
	local EnemyRoot = game["Workspace"]["Units"][newUnit]:WaitForChild("HumanoidRootPart")
	local EnemyPosition = EnemyRoot:WaitForChild("AlignPosition")
	local EnemyOrientation = EnemyRoot:WaitForChild("AlignOrientation")
	
	local NextPath = game["Workspace"]["Units"][newUnit]:FindFirstChild("MovingTo").Value
	
	while wait(0.3) do
		local function FindNearestTarget()
			if config then
				local mobs = workspace.Mobs
				local maxDistance = config.Range.Value
				local maxDistance2 = 1
				local nearestTarget = nil
				local TouchTarget = nil

				for i, target in pairs(mobs:GetChildren()) do
					local distance = (target.HumanoidRootPart.Position - newUnit.HumanoidRootPart.Position).Magnitude
					if not target:FindFirstChild("Config") then 
						continue 
						
					end
					
					if distance < maxDistance then
						nearestTarget = target
						maxDistance = distance
					end
					if distance < maxDistance2 then
						TouchTarget = target
						maxDistance2 = distance
					end
				end
				
				task.wait()
				return nearestTarget, TouchTarget
			end
		end

		local Attack = setAnimation(newUnit, "Attack")

		local target, target2 = FindNearestTarget(), FindNearestTarget()
		print(target, target2)
		
		if target == nil then
		else	
			local UH = config.Health.Value
			
			if target2 == nil then
				
			else
				
				newUnit:WaitForChild("Config", 0.5).Health.Value -= target2:WaitForChild("Config", 0.5).Health.Value
				target2.Config:WaitForChild("Health", 0.5).Value -= newUnit:WaitForChild("Config", 0.5).Health.Value
			end
			
			if newUnit:FindFirstChild("Config") then
				if target2 == nil then
					
				else
					
					newUnit:WaitForChild("Config", 0.5).Health.Value -= target2:WaitForChild("Config", 0.5).Health.Value
					target2.Config:WaitForChild("Health", 0.5).Value -= newUnit:WaitForChild("Config", 0.5).Health.Value
				end
				EnemyRoot.Anchored = true
				newUnit:WaitForChild("Config", 0.5).Speed.Value = 0
				newUnit.Attack.Value = true
				
				wait(newUnit:WaitForChild("Config", 0.5).Cooldown.Value)
				Attack:Play()
				print(Health)
				Health.Value -= newUnit.Config.Damage.Value
				
			end
			EnemyRoot.Anchored = false
			newUnit:WaitForChild("Config", 0.5).Speed.Value = 4
			newUnit.Attack.Value = false
		end
		
		if Health and Health.Value <= 0 then
			break
		end

		if (EnemyRoot.Point.WorldPosition - Path[NextPath].Point.WorldPosition).Magnitude < 1 then
			if NextPath < #Path:GetChildren() then
				NextPath = NextPath + 1
			else
				break
			end
		else
			EnemyPosition.Attachment1 = Path[NextPath].Point
			EnemyPosition.MaxVelocity = UnitConfig.Speed.Value
			EnemyOrientation.Attachment1 = Path[NextPath].Point
		end

		if Health and Health.Value <= 0 then
			break
		end
		task.wait()
	end

	if NextPath >= #Path:GetChildren() then
		newUnit:Destroy()
	end
end

 
function Unit.Spawn(name, quantity, Map, plr)
	local UnitExists = ServerStorage.SpawnTroop:FindFirstChild(name)
	
	local Map = workspace.Map:FindFirstChildOfClass("Folder")	
	
	if UnitExists then 
		for i=1, quantity do
			task.wait(1)
			if Map:FindFirstChild("UnitStart") then
				local newUnit = UnitExists:Clone()
				local range = newUnit:WaitForChild("Config", 0.5).Range.Value
				local height = (newUnit.PrimaryPart.Size.Y/ 2) 
				local offset = CFrame.new(0, -height, 0)

				local rngPart = Instance.new("Part")
				rngPart.Size = Vector3.new(0.1, newUnit.Config.Range.Value, newUnit.Config.Range.Value)
				rngPart.Name = "RangePart"
				rngPart.Shape = Enum.PartType.Cylinder
				rngPart.Material = Enum.Material.SmoothPlastic
				rngPart.Transparency = 1
				rngPart.Color = Color3.new(0.243137, 0.509804, 1)
				rngPart.CFrame = CFrame.new(newUnit.PrimaryPart.CFrame.X, newUnit.PrimaryPart.CFrame.Y - 1, newUnit.PrimaryPart.CFrame.Z) * offset * CFrame.Angles(0, 0, math.rad(90))
				rngPart.Parent = newUnit
				local weld = Instance.new("WeldConstraint")
				weld.Parent = newUnit.HumanoidRootPart
				weld.Part1 = newUnit.HumanoidRootPart
				weld.Part0 = rngPart

				PhysicsService:SetPartCollisionGroup(rngPart, "Air")
				local cframe = CFrame.new(Map.UnitStart.Position)
				newUnit:SetPrimaryPartCFrame(cframe) 
				newUnit.Parent = workspace.Units
				newUnit.Name = plr.DisplayName .. " ".. newUnit.Name

				local UnitConfig = newUnit:FindFirstChild("Config")
				local Path = Map.UnitWaypoints
				local MovingTo = Instance.new("IntValue")
				MovingTo.Name = "MovingTo"
				MovingTo.Parent = newUnit
				MovingTo.Value = 1

				local UnitTag = Instance.new("BoolValue")
				UnitTag.Name = "UnitTag"
				UnitTag.Parent = newUnit

				local Attack = Instance.new("BoolValue")
				Attack.Name = "Attack"
				Attack.Parent = newUnit

				newUnit.PrimaryPart = newUnit.HumanoidRootPart
				newUnit.HumanoidRootPart.Anchored = false
				newUnit.HumanoidRootPart:SetNetworkOwner(nil)	
				newUnit.Config.Speed.Value += 2

				for i, object in ipairs(newUnit:GetDescendants()) do
					if object:IsA("BasePart") then
						object.Anchored = false
						wait(0.1)
						object:SetNetworkOwner(nil)	
						PhysicsService:SetPartCollisionGroup(object, "Units")
					end
				end

				coroutine.wrap(Unit.Move)(newUnit, UnitConfig, Path)

				task.wait(0.1)
				local Idle = setAnimation(newUnit, "Idle")
				local Walk = setAnimation(newUnit, "Walk")
				Walk:Play()

				Attack.Changed:Connect(function(deb)
					if deb == true then
						Walk:Stop()
						Idle:Play()
					else
						Walk:Play()
						Idle:Stop()
					end
				end)
			end
		end	
	else
		warn("Unit Not Exist: ", name)
	end
	
	workspace.Info.Wave.Changed:Connect(function()
		Unit.Spawn("Tropper 2", 1, workspace.Map:FindFirstChildOfClass("Folder"), game.Players:FindFirstChildOfClass("Player"))
	end)

end

return Unit

here is the error:

ServerScriptService.Script.Main.Unit:207: ServerScriptService.Script.Main.Unit:35: invalid argument #2 (string expected, got Instance)

can you show line 35 please? May help

i give the script already, you just need read that
i ask alot ppl but cant fix so i ask forum

1 Like

im reading the script, i want to know where exactly line 35 is.

1 Like

local Health = game[“Workspace”][“Units”][newUnit][UnitConfig]:FindFirstChild(“Health”)

check if any of the Unit stuff is a string and not a number,
example:

Example Number: "8"

to fix that:

tonumber(Variable) or Variable = 8

it a local. if u try print it just give error
not local script*

game["Workspace"]["Units"][newUnit][UnitConfig]:FindFirstChild("Health")

even u try print this it still give error

Try Replacing:

game["Workspace"]["Units"][newUnit][UnitConfig]:FindFirstChild("Health")

with

game["Workspace"]["Units"]["newUnit"]["UnitConfig"]:FindFirstChild("Health")
1 Like

Just use Object.ObjectName instead of Object["ObjectName"], and use workspace instead of game.Workspace. I think it’ll make it easier to see what the problem is.

I don’t really know the full context but i assume UnitConfig is an object, so maybe make the line this:

local Health = UnitConfig:FindFirstChild("Health")

Right now what i think you’re doing is trying to find UnitConfig by finding the child of newUnit using UnitConfig, and passing the second argument as the object. I’m not sure though. I need to know where you’re using Unit.Move.

1 Like

did you know wat is function? the newUnit is not unit name

function Unit.Move(newUnit, UnitConfig, Path)
newUnit Here is object
if u add “newUnit” it will become string
and newUnit is not unit name

well before u say that everyone know use obj.objname not [“objname”] but bc it error so i do that

function Unit.Move(newUnit, UnitConfig, Path)
newUnit Here is object
if u add “newUnit” it will become string
and newUnit is not unit name

try already but still not work
btw unit.move im use in unit.spawn
coroutine.wrap(Unit.Move)(newUnit, UnitConfig, Path)

Oh sorry brother.
Misunderstood that.

After reading your functions i am able to make out that its actually erroring at line 207 :

coroutine.wrap(Unit.Move)(newUnit, UnitConfig, Path)

Bc you are passing 2nd Arg (UnitConfig) as a Instance rather it should be a string since you are using [ ] to reference.

Im not sure if this would work but :

coroutine.wrap(Unit.Move)(newUnit.Name, UnitConfig.Name, Path)

(Replace with line 207)

you forgot to add .Value after the Health IntValue

again newUnit.Name is string not object breh

well im not you should read all the script

try to name it different, it might finding other things with the same name happened to mine

it bad code also i fix it by somehow

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