lastAttack = os.clock()
local humanoid = enemy.Character:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid:TakeDamage(DAMAGE)
end
end
else
-- Wander near base
local offset = Vector3.new(math.random(-10, 10), 0, math.random(-10, 10))
local destination = base.Position + offset
knight.Humanoid:MoveTo(destination)
end
wait(1)
end
end)
end
-- Handle player requesting to buy knight
BuyKnightEvent.OnServerEvent:Connect(function(player)
local cash = player:FindFirstChild("leaderstats") and player.leaderstats:FindFirstChild("Cash")
if not cash or cash.Value < KNIGHT_COST then return end
local base = playerBaseMap[player.UserId]
if not base then return end
local spawnPoint = base:FindFirstChild("KnightSpawn")
if not spawnPoint then return end
-- Deduct cash
cash.Value -= KNIGHT_COST
-- Spawn the knight
local knight = KnightTemplate:Clone()
knight.Parent = workspace
knight:SetPrimaryPartCFrame(spawnPoint.CFrame + Vector3.new(0, 3, 0))
-- Activate AI
setupKnightAI(knight, player, spawnPoint)
end)
-- Example base linking (you must call this from your base assignment script)
Players.PlayerAdded:Connect(function(player)
repeat task.wait() until workspace:FindFirstChild("Bases")
for _, base in pairs(baseFolder:GetChildren()) do
if base:GetAttribute("Owner") == player.UserId then
playerBaseMap[player.UserId] = base
break
end
end
end)
Players.PlayerRemoving:Connect(function(player)
playerBaseMap[player.UserId] = nil
end)