argument 1 missing or nil
i am making a tower defense game but it keeps saying argument 1 missing or nil when i try to spawn the tower this is a module script (error at line 11)
local PhysicsServive = game:GetService("PhysicsService")
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local PhysicsService = game:GetService("PhysicsService")
local events = ReplicatedStorage:WaitForChild("Events")
local Tower = {}
local SpawnTowerEvent = events:WaitForChild("SpawnTower")
function Tower.Spawn(player, Name, CFrame)
local towerExists = 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
PhysicsService:SetPartCollisionGroup(object, "Tower")
object.Material = Enum.Material.ForceField
end
end
else
warn("Missing:", Name)
end
end
SpawnTowerEvent.OnServerEvent:Connect(Tower.Spawn())
return Tower
Solution 1:
SpawnTowerEvent.OnServerEvent:Connect(Tower.Spawn())
Connect
expects a function value, not a function call (unless that function call resolves to a function value). Remove the call operator ()
.
SpawnTowerEvent.OnServerEvent:Connect(Tower.Spawn)
You call Tower.Spawn
without any arguments. Therefor you call FindFirstChild(nil)
which causes the observed error.
Also it does not return a function value.