Function doesnt know parameter

hi, im following a tower defense game tutorial on youtube (gnomecode’s and im on ep 7)

here’s the code:

local physicsService = game:GetService("PhysicsService")
local events = game.ReplicatedStorage:WaitForChild("Events")

local tower = {}

function findNearestTarget(newTower)
	print(newTower)
	local maxDistance = 25
	local nearestTarget = nil

	for i, target in ipairs(workspace.Mobs:GetChildren()) do
		local distance = (target.HumanoidRootPart.Position - newTower.HumanoidRootPart.Position).Magnitude
	
		if distance < maxDistance then
			nearestTarget = target
			maxDistance = distance	
		end
	end

	return nearestTarget
end

function tower.Attack(newTower)
	
	local target = findNearestTarget()
	if target and target:FindFirstChild("Humanoid") and target.Humanoid.Health > 0 then
		
		events:WaitForChild("AnimateTower"):FireAllClients(newTower, "Attack")
		
		target.Humanoid:TakeDamage(10)
	end

	task.wait(1)
	
	tower.Attack(newTower)
end

function tower.Spawn(player, name, cframe)
	local towerExists = game.ReplicatedStorage.Towers:FindFirstChild(name)
	
	if towerExists then
		
		local newTower = towerExists:Clone()
		newTower.HumanoidRootPart.CFrame = cframe
		newTower.Parent = workspace.Towers
		newTower.HumanoidRootPart:SetNetworkOwner(nil)

		for i, object in ipairs(newTower:GetDescendants()) do
			if object:IsA("BasePart") then
				object.CollisionGroup = "Tower"
			end	
		end
		
		coroutine.wrap(tower.Attack)(newTower)
		
	else
		warn("no tower ["..name.."]")
	end
end

events:WaitForChild("SpawnTower").OnServerEvent:Connect(tower.Spawn)

return tower

ive found out that the issue is that the “findNearestTarget” function doesn’t know what the newTower parameter is, ive rewatched the video 3 times and i cannot figure out what i did wrong so help would be appreciated

It looks like the problem with the “findNearestTarget” function is that it expects an argument called “newTower”, which is not being passed to the function when it’s called.

When you call the function in the “Attack” function, make sure to pass in the “newTower” argument:

local target = findNearestTarget(newTower)
1 Like

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